If you are a C/C++ programmer, then you are not used to nesting functions. In JavaScript, you can have one function inside another. In this three-part series, I answer the following questions: can you call a nested function from outside the outermost function? Can a nested function call an outer function? What is the scope within nested functions? I will also show you how to pass arguments between nested functions and look at the relationship between nested functions and the JavaScript object.
Nesting JavaScript Functions - Privacy of Nested Functions (Page 5 of 5 )
As you have seen above, no nested function (independent of its level) can be called from outside the set of the functions. We have also seen that no function can call any function that is more than one level below itself.
Under these two conditions, we say that a nested function is private to its outer function. Since a nested function is private to its nesting (outer) function, the nested function just below the outermost function cannot be called from outside the set of functions (we have said this before). No inner level nested function can be called from outside the set of functions, in accordance with the logic.
Statements Accessing
The JavaScript specification states that "The inner function can be accessed only from statements in the outer function." This means that statements in the outer function can call the inner function; however these statements cannot access the arguments and variables of the inner function.
The calls within the functions above are statements. It just happens that these calls are short statements. For each of these calls you can have a longer statement, within which there is a call to the inner function.
The quotation from the specification is not complete and not explicit. Know that statements in a function cannot call functions that are more than one level lower, and they cannot access arguments and variables that are in any nested function, including the immediate one. A statement in a lower function can call any function above it in the chain. In the next part of this series, I will cover accessing variables and arguments of different functions in the set, in detail.
We have done a lot for this part of the series. It is time to take a break. In the next part of the series we shall look at the scope (accessing) of the variables and arguments in nested functions. See you in the next part.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.