94 Data and Data Types
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).
3. Select Control > Test Movie to see the values display in the Output panel.
4. Now add the following ActionScript after the code you added in step 2:
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.
5. Select Control > Test Movie to see the values display in the Output panel.
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”
on page 94.
Passing a variable by reference
Because the Array and Object data types hold a reference to a value instead of containing its
actual value, you need be careful when you work with arrays and objects.
The following example shows how to pass an object by reference. When you create a copy of
the array, you actually create only a copy of the reference (or alias) to the array’s contents.
When you edit the contents in the second array, you modify both the contents of the first and
second array because they both point to the same value.