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 - Passing Arguments to Inner Functions from Outside (Page 4 of 4 )
From outside the set of functions, you cannot call any of the inner functions; however, you can pass arguments to an inner function. Consider the following code:
Two functions are defined, one nesting within the other. The one outside is called "outside" and has the x parameter. The one inside is called "inside" and has the y parameter. When the HTML button is clicked, the evaluate() function is called. This function has the statement:
result=outside(3)(5);
This statement calls the outside function. Its first argument, 3, is for x of the outside function. Its second argument, 5, is for y of the inside function. Note that each argument is in its own () brackets. The return value from this function is assigned to the result variable. The alert statement in the evaluate() function displays the result.
Now let us see what happens in the set of two functions. When the outer function is called, it receives the argument "3." The role of the outer function here is to do nothing other than call the inner function, and then return the result from the inner function, in one statement. The inner function receives the argument "5" from the system. As we said above, the inner function can see (use) the argument of the outer function. So the inner function uses the argument of the outer function and its own argument to find the sum of the two numbers.
The statement in the inner function finds the sum of the two numbers and returns the answer to whatever function called it. The function that called it is the outer function. There is only one statement for the outer function, and this is the statement that called the inner function. The statement of the outer function receives the answer from the inner function and returns it to the evaluate() function. The alert statement in the evaluate() function displays 8.
So, even though you cannot call any of the inner functions, you can pass arguments to an inner function. Pass the arguments in order according to the way their corresponding functions are nested (in the chain). For the function that does not have an argument, send an empty pair of brackets ().
Time to take a break. In the next part we shall see how nested functions can be used as object types.
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.