Home arrow JavaScript arrow Page 3 - 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 the Functions from Outside
(Page 3 of 5 )

You can call the outermost function from outside the set of functions. You cannot call any of the inner functions from outside the set of functions. The following code illustrates this: 

<html>

<head>

<script type="text/javascript">

function fnName1()

{

alert('Statements 1A')

function fnName2()

{

alert('Statements 2A')

function fnName3()

{

alert('Statements 3')

}

alert('Statements 2B')

}

alert('Statements 1B')

}

</script>

</head>

<body>

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

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

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

</body>

</html>

The set of functions in the example is what we have in the code. There are three HTML buttons. When clicked, the first one will call the outermost function; when clicked, the second button should call the middle function (but it would fail); when clicked, the last button should call the innermost function (but it would fail).

Save the above code as an HTML file. Open it with your browser and click the buttons to confirm which functions are called. The inner functions will not be called; only the outer function will be called. This is the behavior of nested functions.

When the outermost function is called, by clicking its button, an alert box displays "Statements 1A." The nested functions cannot be called from outside the set of functions. When you click the second and third button to attempt this, the browser may indicate an error message at its status bar.


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