So heres the basic outline
function x(){
// some code
function y(){
//some more code
}
}
function z(){
// how do i call function y?
}
I tried
function z(){
window[x][y];
}
and
function z(){
x();y();
}
neither works!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Lots of code, not much explanation.
The above declares y inside x, so it is created as a property of x‘s variable object each time x is called. y can only be accessed from inside x unless code inside x makes it available from elsewhere.
To call y from inside z, it must be available on z‘s scope chain. That can be done by passing it in the function call (making it a property of z‘s variable object) or making it a property of some object on z‘s scope chain.
If the function is to be available to both functions, it makes sense to declare it where it can be accessed by both x and z, or initialize z in such a manner that y is available. e.g.
In the above, x and z both have access to the same y function and it is not created each time x is called. Note that z will be undefined until the code assigning to x is executed.
Note also that y is only available to x and z, it can’t be accessed by any other function (so y might be called a private function and x and z might be called privileged functions).