
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
58
|
Chapter 2: Variables
When a variable is referenced in a frame script, Flash checks the current movie clip
for the variable. If the variable is not found in the movie clip (or its prototype), Flash
checks for a global variable of that name. If no global variable is found, Flash returns
undefined. When both a movie clip variable and a global variable of the same name
are defined, Flash retrieves the movie clip variable, because it is first in the search
order for the scope chain. Hence, a movie clip variable always overrides a global vari-
able of the same name.
By contrast, the scope chain for code in a nonnested function includes three scopes
(again, read from the bottom up):
• Global object (
_global)
• Enclosing movie clip object
• Local variables
When a variable is referenced from within a function, Flash checks first for a local
variable—defined with the var statement—within the function, as shown in
Example 2-5. If no local variable is found, Flash checks for a movie clip variable in
the movie clip in which the function was defined. (We’ll consider this topic, includ-
ing the behavior for nested functions, more thoroughly in Chapter 9.) If the variable
is not found in that movie clip (or its prototype), Flash checks for a global variable of
the same name. If no global variable is ...