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.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...