Variables, Functions, and Flow Control - 4.3 Nesting Named Functions (Page 3 of 4 )
Problem
You want to create a function that belongs to only one other function.
Solution
You can nest a function inside another function according to the following syntax model:
function myFuncA() { // statements go here function.myFuncB() { // more statements go here } }
In this construction, the nested function may be accessed only by statements in the outer function. Statements in the nested function have access to variables declared in the outer function, as well as to global variables. Statements in the outer function, however, do not have access to the inner function's variables.
Discussion
The basic idea behind nested functions is that you can encapsulate all activity related to the outer function by keeping subroutine functions private to the outer function. Because the nested function is not directly exposed to the global space, you can reuse the function name in the global space or for a nested function inside some other outer function.