Scope of Variables and Arguments in Nested JavaScript Functions
In this part of the series we shall look at the scope of variables and arguments in nested functions. We shall see whether the outer function can use the variables and arguments of the inner functions and vice versa. We shall also see how many levels downward or upward this can be done.
Scope of Variables and Arguments in Nested JavaScript Functions - Function Call, Argument and Variable Scope Summary (Page 3 of 4 )
We conclude that a function can call its immediate nested function, but it cannot call a function that is more than one level down (inner). However, any nested function can call its nesting (outer) function or any of its ancestor outer functions.
Independent of the level of the inner function, an outer function cannot use the variables of an inner function. Even though an outer function can call its immediate inner function, an outer function cannot use the variables of its immediate inner function.
Independent of the level of the inner function, an outer function cannot use the arguments of the inner function. Even though an outer function can call its immediate inner function, an outer function cannot use the arguments of its immediate inner function.
Independent of the level of the outer function, an inner function can use the variables of the outer function. Independent of the level of the outer function, the inner function can use the arguments of the outer function.
Calls Within the Set of Functions
As we have seen, functions can be calling one another within the set. However, watch out for unending loops. You should use nested functions instead of independent functions when you are looking for something like a small module. In this module situation, you would call the outermost function; the set of nested functions will carry out the task, and then only return the result. It is the outermost function that has to return the result.