Nesting JavaScript Functions
(Page 1 of 5 )
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.
You should have basic knowledge of JavaScript to understand this series. There are samples of code in the article series that you can try.
The Nature of Nesting Functions
I show you three functions here. One is nesting within another, which is nesting within a third. I show just the outline of the functions.
function fnName1(parameters1)
{
optional statements1A
function fnName2(parameters2)
{
optional statements2A
function fnName3(parameters3)
{
optional statements3
}
optional statements2B
}
optional statements1B
}
You have the outermost function, named "fnName1." This function can have statements (statements1A and statements1B). The function named "fnName2" is nested inside the fnName1 function. It can be put in front of the statements of the fnName1 function (before statement1A), within the statements (between statements1A and statements1B) or after the statements (after statements1B).
The function named "fnName2" has its own statements (statements2A and statements2B). The function named "fnName3" is inside the fnName2 function. It can be put in front of the statements of the fnName2 function, within the statements or after the statements.
The function named "fnName3" has its own statements. All of the statements in any of the functions are optional. There can be more than one function at each inner level (we shall look at that in the last part of the series).
Next: Tutorial Organization >>
More JavaScript Articles
More By Chrysanthus Forcha