
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
206
|
Chapter 9: Functions
Nested functions are often used to encapsulate a series of complex operations into a
single package, while preventing the outside world from gaining access to its internal
workings. For an interesting example of nested functions (sometimes called inner
functions) used to simulate Java-style private members, see:
http://www.crockford.com/javascript/private.html
Nested Function Scope
When a nested function executes, the interpreter adds the nested function’s local
scope to the scope chain of its parent function. For example, in Example 9-10 the
scope chain of showFullName( ) is:
• Global object (interpreter ends its search for variables here)
• Enclosing movie clip object (where showFullName( ) was defined)
• showFullName( ) local scope (interpreter starts its search for variables here)
And the scope chain of showFirstName( ) is
• Global object (interpreter ends its search for variables here)
• Enclosing movie clip object (where showFullName( ) was defined)
• showFullName( ) local scope
• showFirstName( ) local scope (interpreter starts its search for variables here)
The scope of the nested function is the same as its parent’s with the addition of its
own local scope. If a variable does not exist in showFirstName( )’s local scope, the
interpreter will check showFullName( ...