44
Chapter 2: ActionScript Basics
As another example, the variable
inValue
contains a primitive value, 3, so the actual value is
passed to the
sqrt()
function and the returned value is 9:
function sqrt(x){
return x * x;
}
var inValue = 3;
var out = sqrt(inValue);
The value of the variable
inValue
does not change.
The object data type can contain such a large and complex amount of information that a variable
with this type doesn’t hold the actual value; it holds a reference to the value. This reference is like
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.
The following is an example of passing by reference:
var myArray = ["tom", "josie"];
var newArray = myArray;
myArray[1] = "jack";
trace(newArray);
The above code creates an Array object called
myArray
that has two elements. The variable
newArray
is created and is passed a reference to
myArray
. When the second element of
myArray
is
changed, it affects every variable with a reference to it. The
trace()
action sends
tom, jack
to
the Output panel.
In the following example,
myArray
contains an Array object, so it is passed to function
zeroArray()
by reference. The
zeroArray()
function changes the content of the array
in
myArray
.
function zeroArray (theArray){
var i;
for (i=0; i < theArray.length; i++) {
theArray[i] = 0;
}
}
var myArray = new Array();
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
zeroArray(myArray);
The function
zeroArray()
accepts an Array object as a parameter and sets all the elements of
that array to 0. It can modify the array because the array is passed by reference.
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...