Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 98

Programming actionscript 3.0
Table of Contents

Advertisement

In ActionScript 3.0, all arguments are passed by reference because all values are stored as
objects. However, objects that belong to the primitive data types, which includes Boolean,
Number, int, uint, and String, have special operators that make them behave as if they were
passed by value. For example, the following code creates a function named
that defines two parameters named
passPrimitives()
These parameters are similar to local variables declared inside the body of the
function. When the function is called with the arguments
passPrimitives()
, the parameters
yValue
represented by
xValue
passed by value. Although
and
objects, any changes to the variables within the function body generate new
yValue
copies of the values in memory.
function passPrimitives(xParam:int, yParam:int):void
{
xParam++;
yParam++;
trace(xParam, yParam);
}
var xValue:int = 10;
var yValue:int = 15;
trace(xValue, yValue);
passPrimitives(xValue, yValue); // 11 16
trace(xValue, yValue);
Within the
passPrimitives()
but this does not affect the values of
statement. This would be true even if the parameters were named identically to the variables,
and
, because the
xValue
yValue
locations in memory that exist separately from the variables of the same name outside the
function.
All other objects—that is, objects that do not belong to the primitive data types—are always
passed by reference, which gives you ability to change the value of the original variable. For
example, the following code creates an object named
The object is passed as an argument to the
primitive type, the object is not only passed by reference, but also stays a reference. This
means that changes made to the parameters within the function will affect the object
properties outside the function.
function passByRef(objParam:Object):void
{
objParam.x++;
objParam.y++;
98
ActionScript Language and Syntax
and
xParam
yParam
and
. Because the arguments are primitives, they behave as if
yValue
and
xParam
yParam
// 10 15
// 10 15
function, the values of
xValue
and
xValue
passByRef()
and
xParam
are initialized with references to the int objects
initially contain only references to the
and
xParam
and
, as shown in the last
yValue
inside the function would point to new
yValue
with two properties,
objVar
function. Because the object is not a
, both of type int.
yParam
and
xValue
xValue
are incremented,
yParam
trace
and
x
y
.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents