I have created nested function in one script file A.js, like below:
function A(){
function B(){}
}
If I want to call function B() in another script file C.js, what should I do?
What you want to do seems to be to create a function closure of B using the variables within A(). You can then access this closure B if you return B after you call A(). Then, in C.js, you can access B by calling A and using the return value:
A.js:
function A([parameters]) {
[variables]
function B() { [function body] }
//Return B from A:
return B;
}
C.js:
//Get B:
var bFunction = A([parameters]):
//Call bFunction:
bFunction();
You have to return the nested function B() from the parent function A().
Your code
function A(){
function B(){}
}
Updated Code with return statement
function A(){
return function B(){ //return added
}
}
You can access the child function by adding an additional () Parentheses to the function call like the below example.
A()();
BfromA(). Longer answer coming up!