User Guide
Functions 101
You should also be careful to avoid using the string “arguments” as a parameter name
because it will shadow the
arguments object. For example, if the function traceArgArray()
is rewritten so that an arguments parameter is added, the references to arguments in the
function body refer to the parameter rather than the
arguments object. The following code
produces no output:
function traceArgArray(x:int, arguments:int):void
{
for (var i:uint = 0; i < arguments.length; i++)
{
trace(arguments[i]);
}
}
traceArgArray(1, 2, 3);
// no output
The arguments object in previous versions of ActionScript also contained a property named
caller, which is a reference to the function that called the current function. The caller
property is not present in ActionScript 3.0, but if you need a reference to the calling function,
you can alter the calling function so that it passes an extra parameter that is a reference to
itself.
The ... (rest) parameter
ActionScript 3.0 introduces a new parameter declaration called the ... (rest) parameter. This
parameter allows you to specify an array parameter that accepts any number of comma-
delimited arguments. The parameter can have any name that is not a reserved word. This
parameter declaration must be the last parameter specified. Use of this parameter makes the
arguments object unavailable. Although the ... (rest) parameter gives you the same
functionality as the
arguments array and arguments.length property, it does not provide
functionality similar to that provided by
arguments.callee. You should ensure that you do
not need to use
arguments.callee before using the ... (rest) parameter.
The following example rewrites the
traceArgArray() function using the ... (rest) parameter
instead of the
arguments object:
function traceArgArray(... args):void
{
for (var i:uint = 0; i < args.length; i++)
{
trace(args[i]);
}
}
traceArgArray(1, 2, 3);