MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT Reference Manual page 44

Actionscript reference guide
Hide thumbs Also See for FLASH MX 2004 - ACTIONSCRIPT:
Table of Contents

Advertisement

As another example, the variable
passed to the
sqrt()
function sqrt(x){
return x * x;
}
var inValue = 3;
var out = sqrt(inValue);
The value of the variable
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
is created and is passed a reference to
newArray
changed, it affects every variable with a reference to it. The
the Output panel.
In the following example,
by reference. The
zeroArray()
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()
that array to 0. It can modify the array because the array is passed by reference.
44
Chapter 2: ActionScript Basics
contains a primitive value, 3, so the actual value is
inValue
function and the returned value is 9:
does not change.
inValue
contains an Array object, so it is passed to function
myArray
zeroArray()
accepts an Array object as a parameter and sets all the elements of
that has two elements. The variable
myArray
. When the second element of
myArray
trace()
function changes the content of the array
myArray
action sends
tom, jack
is
to

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?

Questions and answers

Table of Contents