Pass Javascript getter as parameter

I have an ecma6 / es2015 class with a getter defined like this:

get foo() { return this._foo; } 

What I would like to do is pass this function as a parameter. The call is this:

 someFunction(myClass.foo); 

just call the function. Is there a clean way to pass a method without calling it, and then call in the passage in which I pass it?

+6
source share
2 answers

I assume that you will have to wrap it in an anonymous function so that it doesn't execute:

 someFunction(() => myClass.foo); 

Or you can get getter function, but it is less readable than above:

 someFunction(Object.getOwnPropertyDescriptor(myClass, 'foo').get); 
+8
source

Not sure if I understood that you mean a way to define a function and pass it later to another (as a function) that will be called later?

something like that?

 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> 
0
source

All Articles