Home arrow JavaScript arrow Page 2 - 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 - Tutorial Organization
(Page 2 of 5 )

The example described above is similar to one person having a child, who then also has a child -- a grandparent, parent, and child. It's three people, all in one line. You have a function nesting within one function, which is also nesting within another function.

This is not the only way to have nested functions. Another way is to have a function with one or more functions immediately nested, and then each of these nested (inner) functions would have one or more functions immediately nested. This goes down until you end up with a tree-like structure.

We shall not look at the tree-like nested functions in this series. The JavaScript 1.5 reference does not say anything explicitly about the-tree like structure. We shall use the single line structure to explain most of the features in this series; wherever we need to have more than one function nested at the same level, I will indicate that to you.

In this part of the series we shall see how the functions can be called. In the next part of the series we shall look at the scope of arguments and variables within the functions. In the third and last part of the series, we shall see how we can use a set of nested functions in a JavaScript object type.

Example

This is the example we shall use to illustrate most of the principles. We shall be modifying the code as we go along.

function fnName1()

{

alert('Statements 1A')

function fnName2()

{

alert('Statements 2A')

function fnName3()

{

alert('Statements 3')

}

alert('Statements 2B')

}

alert('Statements 1B')

}

For simplicity, where the statements are to occur, I have just written an alert statement that displays a message indicating the nesting level of the statements. Please read through the function to appreciate what I have said.


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 11 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials