Home arrow JavaScript arrow Page 4 - Nesting JavaScript Functions
JAVASCRIPT

Nesting JavaScript Functions


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.

Author Info:
By: Chrysanthus Forcha
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
October 07, 2009
TABLE OF CONTENTS:
  1. · Nesting JavaScript Functions
  2. · Tutorial Organization
  3. · Calling the Functions from Outside
  4. · Calling Functions from Inside
  5. · Privacy of Nested Functions

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Nesting JavaScript Functions - Calling Functions from Inside
(Page 4 of 5 )

A function can call its immediately nested (inner) function, but it cannot call a function that is more than one (inner) level down. So, in the above example, if the outer function is called, the innermost function cannot be called unless the outermost function calls the middle function, which then calls the innermost function. In the following code (a modified form of the earlier example), the outermost function is called; it calls the middle function, which calls the innermost function.

<html>

<head>

<script type="text/javascript">

function fnName1()

{

alert('Statements 1A');

function fnName2()

{

alert('Statements 2A');

function fnName3()

{

alert('Statements 3');

}

alert('Statements 2B');

fnName3();

}

alert('Statements 1B');

fnName2();

}

</script>

</head>

<body>

<button type="button" onclick="fnName1()">Call fnName1 Function</button>

</body>

</html>

Look at the position of the function calls. When the outermost function is called as a result of clicking the button (the only button in the code), the first alert box of the outermost function will display "Statements 1A" and the second alert box (look at its position in the code) in fnName1() will display "Statements 1B." After this, the middle function is called, because the call "fnName2();" is immediately after the second alert statement in the outermost function code.

When the middle function is called, the first alert box of the middle function will display "Statements 2A" and the second alert box (note its position in the code) will display "Statements 2B." A nested function cannot be executed unless it is called.

The nested function definition and some statements can be at the same level in the code. The definition of the fnName2() function and its call below it (after the second alert statement) are at the same statement level. It can only be so, since a function can only call its immediate nested function.

After the second alert statement in the middle function is executed, the innermost function is called, because the call "fnName3();" occurs immediately after the second alert statement. You can try the above code.

When the innermost function is called, its single alert statement displays "Statements 3."

Now try the following code and you will realize that nothing will happen or an error will be displayed in the status bar of your browser.

<html>

<head>

<script type="text/javascript">

function fnName1()

{

fnName3();

function fnName2()

{

alert('Statements 2A');

function fnName3()

{

alert('Statements 3');

}

alert('Statements 2B');

}

}

</script>

</head>

<body>

<button type="button" onclick="fnName1()">Call fnName1 Function</button>

</body>

</html>

When you click the button, the outermost function will be called. As soon as it is called, it tries to call the innermost function (fnName3()). When it attempts that, however, nothing happens, or you see an error message in the task bar of your browser.

We conclude that a function can call its immediate nested function, but it cannot call a function that is more than one level down. However, any nested function can call its nesting (outer) function or any of its ancestor outer functions; that is, you can have a call (statement) in a nested function that will do the calls. Well, this can lead to a loop of calling functions, when a nesting function calls a nested function and the nested function calls some ancestor function. You should avoid having this type of loop situation in your code, unless you have a very good reason for it.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials