Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Data and Data Types > About variables > Using variables in an application | |||
In this section, you use variables in short code snippets of ActionScript. You need to declare and initialize a variable in a script before you can use it in an expression. Expressions are combinations of operands and operators that represent a value. For example, in the expression i+2, i and 2 are operands, and + is an operator.
If you do not initialize a variable before you use it in an expression, the variable is undefined and may cause unexpected results. For more information on writing expressions, see Syntax and Language Fundamentals.
If you use an undefined variable, as shown in the following example, the variable's value in Flash Player 7 and later will be NaN, and your script might produce unintended results:
var squared:Number = myNum * myNum; trace(squared); // NaN var myNum:Number = 6;
In the following example, the statement that declares and initializes the variable myNum comes first, so squared can be replaced with a value:
var myNum:Number = 6; var squared:Number = myNum * myNum; trace(squared); // 36
Similar behavior occurs when you pass an undefined variable to a method or function, as shown next.
// Does not work
function badClickListener(evt:Object):Void {
getURL(targetUrl);
var targetUrl:String = "http://www.adobe.com";
}
bad_button.addEventListener("click", badClickListener);
// Works
function goodClickListener(evt:Object):Void {
var targetUrl:String = "http://www.adobe.com";
getURL(targetUrl);
}
good_button.addEventListener("click", goodClickListener);
This button properly opens the web page.
The type of data that a variable contains affects how and when the variable's value changes. Primitive data types, such as strings and numbers, are passed by value, which means the current value of the variable is used rather than a reference to that value. Examples of complex data types include the Array and Object data types.
In the following example, you set myNum to 15 and copy the value into otherNum. When you change myNum to 30 (in line 3 of the code), the value of otherNum remains 15 because otherNum doesn't look to myNum for its value. The otherNum variable contains the value of myNum that it receives (in line 2 of the code).
var myNum:Number = 15; var otherNum:Number = myNum; myNum = 30; trace(myNum); // 30 trace(otherNum); // 15
When you change myNum to 30 (in line 3 of the code), the value of otherNum remains 15 because otherNum doesn't look to myNum for its value. The otherNum variable contains the value of myNum that it receives (in line 2 of the code).
function sqr(myNum:Number):Number {
myNum *= myNum;
return myNum;
}
var inValue:Number = 3;
var outValue:Number = sqr(inValue);
trace(inValue); // 3
trace(outValue); // 9
In the this code, the variable inValue contains a primitive value, 3, so the value passes to the sqr() function, and the returned value is 9. The value of the variable inValue does not change, although the value of myNum in the function changes.
The Object data type can contain such a large amount of complex information that a variable with this type doesn't hold an actual value; it holds a reference to a value. This reference is similar to an alias that points to the contents of the variable. When the variable needs to know its value, the reference asks for the contents and returns the answer without transferring the value to the variable.
For information on passing a variable by reference, see Passing a variable by reference.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000660.html