Home arrow JavaScript arrow Page 5 - 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 - 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.

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