Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - How do I call a function inside of a function?

Tags:

javascript

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.

like image 929
CraigWl Avatar asked May 17 '10 16:05

CraigWl


People also ask

Can I call a function inside the same function JavaScript?

You can call the same callback function again until condition is true : someFunction(function repeat(result) { if (result. winner) { someFunction(repeat); } });

Can you call a function within another function?

Calling a function from within itself is called recursion and the simple answer is, yes.

Can you call a nested function?

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.

Can you call a nested function in JavaScript?

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).


1 Answers

See exposing inner functions to the outer world.

like image 158
KMån Avatar answered Nov 14 '22 23:11

KMån