Sunday, December 2, 2012

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 commadelimited 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);
// 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

0 komentar:

Post a Comment

Programming Actionscript 3. Powered by Blogger.