Javascript closure: dynamically-defined functions, call method from current scope -


is possible have static function calls dynamically defined function? need because can't change static function, , know calls dynamic function. example:

function staticfunc() {   dynamicfunc(); }  function test() {   function dynamicfunc() {console.log('yay');}   staticfunc(); };  test(); 

but gives me error dynamicfunc not defined.

i know if hardcode staticfunc in test(), works. noticed error when call staticfunc, not when define (even though dynamicfunc not defined yet), makes seem staticfunc running inside test()'s scope, evidently not.

any way this? way can think of make global function funcptr gets assigned dynamic function.

closure can use variable of parent level scope in child level scope. here in case reverse have initialize function in child level scope , try invoke in parent scope not correct.

you may pass current function parameter , invoke it:

here demo

function staticfunc(dynamicfunc) {   dynamicfunc(); }  function test() {   var dynamicfunc = function() {console.log('yay');}   staticfunc(dynamicfunc); };  test(); 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -