About variables 41
To test the value of a variable, use the trace() action to send the value to the Output panel. For
example,
trace(hoursWorked) sends the value of the variable hoursWorked to the Output panel
in test mode. You can also check and set the variable values in the Debugger in test mode. For
more information, see “Using the trace statement” on page 79 and “Displaying and modifying
variables” on page 72.
Naming a variable
A variable’s name must follow these rules:
• It must be an identifier (see “Terminology” on page 26).
• It cannot be a keyword or an ActionScript literal such as true, false, null, or undefined.
• It must be unique within its scope (see “Scoping and declaring variables” on page 41).
Also, you should not use any element in the ActionScript language as a variable name; doing so
can cause syntax errors or unexpected results. For example, if you name a variable
String and
then try to create a String object using
new String(), the new object is undefined.
hello_str = new String();
trace(hello_str.length); // returns 0
String = "hello"; // Giving a variable the same name as a built-in class
hello_str = new String();
trace(hello_str.length); // returns undefined
The ActionScript editor supports code hints for built-in classes and for variables that are based on
these classes. If you want Flash to provide code hints for a particular object type that is assigned to
a variable, you can strictly type the variable or name the variable using a specific suffix.
For example, suppose you type the following code:
var members:Array = new Array();
members.
As soon as you type the period (.), Flash displays a list of methods and properties available for
Array objects. For more information, see “Writing code that triggers code hints” on page 61.
Scoping and declaring variables
A variable’s scope refers to the area in which the variable is known and can be referenced. There are
three types of variable scope in ActionScript:
• Local variables are available within the function body in which they are declared (delineated by
curly braces).
• Timeline variables are available to any script on that Timeline.
• Global variables and functions are visible to every Timeline and scope in your document.
Note: ActionScript 2.0 classes that you create support public, private, and static variable scopes. For
more information, see “Controlling member access” on page 164 and “Creating class members”
on page 165.