I have the following JavaScript code:
function parentFunc() {
function childFunc() {
...
}
...
}
// outside of myParentFunc, how do I call myChildFunc() ?
childFunc(); // doesn't work
How do I call childFunc()
from outside of parentFunc()
?
UPDATE:
I know the obvious answer would be to move childFun
outside of parentFun
, but this is a third party library that I can't modify.
You can call the same callback function again until condition is true : someFunction(function repeat(result) { if (result. winner) { someFunction(repeat); } });
Calling a function from within itself is called recursion and the simple answer is, yes.
You must call a nested function either directly by name (without using feval ), or using a function handle that you created using the @ operator (and not str2func ). All of the variables in nested functions or the functions that contain them must be explicitly defined.
Only the containing function can access the nested function. We cannot access it anywhere outside the function. This is because the inner function is defined in the scope of the outer function (or containing function).
See exposing inner functions to the outer world.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With