
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Variable Scope
|
57
preter creates a movie clip (nonlocal) variable named height and sets its value to 10.
As a nonlocal variable,
height persists even after the function finishes.
Example 2-4 demonstrates local and nonlocal variable usage.
Note that it is possible (though confusing and ill-advised) to have both a local and a
nonlocal variable that share the same name within a script but have different scopes.
Example 2-5 shows such a case.
The Scope Chain
Internally, Flash resolves each unqualified variable reference in our code using a
scope chain, which is simply a hierarchy of places (scopes) to look when a reference
to a variable is resolved (i.e., when the correct variable is located unambiguously and
its value is retrieved). Whenever Flash encounters a variable in a script or function, it
seeks the variable’s value in the scope chain associated with that code. The hunt for
the variable ends when the variable is found somewhere in the scope chain or when
it is not found at all (the variable is
undefined.) The scope chain for a frame script
(code attached directly to a movie clip keyframe) includes two scopes, which are tra-
ditionally read from the bottom up:
• Global object (interpreter looks here last)
• Enclosing movie clip object (interpreter looks here first)
Example 2-4. Local and ...