admin管理员组文章数量:1355675
I have an ecma6/es2015 class with a getter defined like so:
get foo() { return this._foo; }
What I'd like to be able to do is pass that function as a parameter. Making a call like so:
someFunction(myClass.foo);
will simply invoke the function. Is there a clean way I can pass the method without invoking it and then invoke in the pass I'm passing it into?
I have an ecma6/es2015 class with a getter defined like so:
get foo() { return this._foo; }
What I'd like to be able to do is pass that function as a parameter. Making a call like so:
someFunction(myClass.foo);
will simply invoke the function. Is there a clean way I can pass the method without invoking it and then invoke in the pass I'm passing it into?
Share Improve this question asked Mar 9, 2016 at 1:56 Siegmund NagelSiegmund Nagel 1,3812 gold badges11 silver badges20 bronze badges2 Answers
Reset to default 11I assume you'll have to wrap it into an anonymous function to keep it from getting executed:
someFunction(() => myClass.foo);
Or, you can get the getter function itself, but it is less readable than the above:
someFunction(Object.getOwnPropertyDescriptor(myClass, 'foo').get);
Not sure I understood you do you mean a way to define the function and pass it later to another one (as a function) to be invoked later?
something like this?
var myFunc = function() {
console.log('func');
document.getElementById('result').innerHTML='func';
}
console.log( 'start');
document.getElementById('result').innerHTML='start';
setTimeout( myFunc, 3000 );
console.log( 'end');
document.getElementById('result').innerHTML='end';
<h1 id='result'>default</h1>
本文标签: ecmascript 6Pass a Javascript getter as a parameterStack Overflow
版权声明:本文标题:ecmascript 6 - Pass a Javascript getter as a parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744037365a2580024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论