User Guide
102 ActionScript Language and Syntax
// output:
// 1
// 2
// 3
The ... (rest) parameter can also be used with other parameters, as long as it is the last
parameter listed. The following example modifies the
traceArgArray() function so that its
first parameter,
x, is of type int, and the second parameter uses the ... (rest) parameter. The
output skips the first value because the first parameter is no longer part of the array created by
the ... (rest) parameter.
function traceArgArray(x: int, ... args)
{
for (var i:uint = 0; i < args.length; i++)
{
trace(args[i]);
}
}
traceArgArray(1, 2, 3);
// output:
// 2
// 3
Functions as objects
Functions in ActionScript 3.0 are objects. When you create a function, you are creating an
object that can not only be passed as a parameter to another function, but also have properties
and methods attached to it.
Functions passed as arguments to another function are passed by reference and not by value.
When you pass a function as an argument, you use only the identifier and not the parentheses
operator that you use to call the method. For example, the following code passes a function
named
clickListener() as an argument to the addEventListener() method:
addEventListener(MouseEvent.CLICK, clickListener);
The Array.sort() method also defines a parameter that accepts a function. For an example
of a custom sort function that is used as an argument to the
Array.sort() function, see
“Sorting an array” on page 231.