O'Reilly logo

ActionScript: The Definitive Guide by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required