Function Scope
ActionScript statements have
a certain scope, or area
of effect within which they are valid. When a statement is attached
to a movie clip, that statement’s scope is limited to the clip
that bears it. For example, here we refer to a variable,
score
, in an assignment statement:
score = 10;
If that statement were attached to clipA
, then the
interpreter would set the value of score
in
clipA
, because the statement is
“scoped” to clipA
. If that statement
were attached to clipB
, the interpreter would set
the value of score
in clipB
,
because the statement is scoped to clipB
. The
location of the statement determines its scope, and, hence, its
effect.
Statements in the body of a function operate in their own, separate scope, called a local scope. A function’s local scope is like a private phone booth for the function, distinct from the scope of the clip or object to which the function is attached. The local scope of a function is created when the function is invoked and destroyed when the function finishes executing. When resolving variables referenced in the statements of the function body, the interpreter looks first in the function’s scope.
Function parameters, for example, are defined in the local scope of a function—not the scope of the timeline that bears the function. Parameters, hence, are accessible to the statements of a function’s body only while the function is running. Statements outside the function have no access to the function’s parameters.
A function’s local scope ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.