
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Function Scope
|
195
For example, here we issue a statement that creates a variable, score:
var score = 10;
If this statement were attached to clipA, the interpreter would set the value of score
in clipA. If the statement were attached to clipB, the interpreter would set the value
of
score in clipB.
Statements in the body of a function operate in their own, separate scope, called a
local scope. A function’s local scope is private to the function, distinct from the scope
of the clip or object to which the function is attached. A new local scope for the func-
tion is created each time the function is invoked and destroyed when the function
finishes executing. When resolving variables referenced in the statements of the func-
tion body, the interpreter looks first in the function’s local scope.
Function parameters, for example, are defined in the local scope of a function—not
on the timeline that bears the function. Hence, parameters are accessible only to the
statements of a function’s body and only while the function is running. Statements
outside the function have no access to the function’s parameters.
A function’s local scope provides a place for temporary “local” variables used solely
within a function. This eliminates potential name conflicts between local function
variables and timeline ...