96 Data and Data Types
3. Select Control > Test Movie to test your ActionScript.
The first
trace() statement in this ActionScript displays the original contents of the
myArray array (1,2,3). After you call the zeroArray() function and pass a reference to
the
myArray array, each of the array’s values are overwritten and set to zero. The
subsequent
trace() statement displays the new contents of the myArray array (0,0,0).
Because you pass the array by reference and not by value, you don’t need to return the
updated contents of the array from within the
zeroArray() function.
For more information on arrays, see “About arrays” on page 163.
About variables and scope
A variable’s scope refers to the area in which the variable is known (defined) and can be
referenced. The area in which the variable is known might be within a certain timeline or
inside a function, or it might be globally known throughout the entire application.For more
information about scope, see “About scope and targeting” on page 123.
Understanding variable scope is important when you develop Flash applications with
ActionScript. Scope indicates not only when and where you can refer to variables but also for
how long a particular variable exists in an application. When you define variables in the body
of a function, they cease to exist as soon as the specified function ends. If you try to refer to
objects in the wrong scope or to variables that have expired, you get errors in your Flash
documents, which lead to unexpected behavior or broken functionality.
There are three types of variable scopes in ActionScript:
■ Global variables and functions are visible to every timeline and scope in your document.
Therefore, a global variable is defined in all areas of your code.
■ Timeline variables are available to any script on that timeline.
■ Local variables are available within the function body in which they are declared
(delineated by curly braces). Therefore, local variables are only defined in a part of
your code.
For guidelines on using scope and variables, see Chapter 5, “About scope and targeting,” on
page 123.
You cannot strict type global variables. For information and a workaround, see “Global
variables” on page 97.
NOTE
ActionScript 2.0 classes that you create support public, private, and static variable
scopes. For more information, see “About class members” on page 250 and
“Controlling member access in your classes” on page 273.