Home >
About Advanced Flash Tactics
Advanced Flash Tactics or AFTs are techniques that come from deep within the Flash Art Of War, the oldest Flash military treatise in the world. Each AFT is designed to be quickly digested, usually only taking a few minutes to get up and running, and contains valuable information you can directly apply to your next Flash campaign. In this AFT I will go over - AS 3's Rest Parameter.
The rest parameter is a welcome addition to Action Script 3, lets take a look at it's definition:
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.
Lets take a look at a method that has a rest parameter in it:
function getItems(...rest):void
{
// ... logic goes here
}
Notice that the only parameter the getItems method is ...rest, this is a rest parameter. When creating a rest parameter you should keep the following things in mind:
- Rest parameters are untyped. It is up to you to validate any special type requirements as you loop through the rest parameter array.
- Rest parameters must be at the end of a method's parameters.
- The rest parameters must have ... in front of it, but the variable name can be anything.
When you want to retrieve values out of a rest parameter you simply iterate (loop) through them like you would any other array. Here is an example:
function getItems(...items):void
{
var total:int = items.length;
for( var i:int = 0; i < total; i++)
{
trace("Look up item", items[i]);
}
}
getItems("testA","testB","testC", 10);
As you can see in the above example we create a method with a rest parameter. When we call the method we can pass in any number of items as long as they are separated by a comma. Back in the getItems method we get the total from the rest parameter just like we would an array.
Pay attention to the trace statement, do you notice anything special about it? I am actually tracing out two separate values and using a comma to separate them. This entire time you have been able to use rest statements in trace and probably didn't know it! By using a comma to break up values in the trace call, a space is automatically added between them in the output window.
So this covers the basics of how to use rest parameters. Before you go off and try this on your own there is one more thing you may want to know. How do you convert an array into rest parameter?
Your first thought may be to simply call the method and pass in the array like any other value. If you do this however the first rest parameter will simply be an array containing it's own elements. Check out this quick example:
function getItems(...items):void
{
trace("Total rest parameters", items.length);
}
var list:Array = new Array("testA","testB","testC");
getItems(list);
function getItems(...items):void
{
trace("Total rest parameters", items.length);
}
var list:Array = new Array("testA","testB","testC");
getItems.apply(this, list);
Notice how I am using the apply function on the getItems method? This may seem incredibly crazy if you have never heard of the apply method before; let me explain. In object oriented programing everything is technically an object. Objects can have methods attached to them, so far so good? Since methods are objects as well, AS 3 methods have functions of their own.
I won't get into to much detail on this but for the purpose of this example it's important to understand that the apply function will correctly pass in our array, or any value for that matter, into the method you use it on. The first parameter of the apply method deals with scope and since this is a basic example we can set it to this. If you want to learn more about apply and other method functions, check out Adobe's documentation here.
So that covers rest parameters in ActionScript 3. When used properly it can be a very powerful tool for creating dynamic class APIs. If you have any questions or problems please feel free to leave a comment below and I'll do my best to answer them.




Facebook Application Development
Also you have "arguments"
S.
The last example throws a compiler error as the function does not return anything when it was typed to return an Array.
Thanks for the heads up on the last example, I just fixed it.
Just corrected a slight mixup where I used call instead of apply, thanks Andy D.
Nice article Jeese! :)
The arguments property only exists when parameter list is fixed. i.e:
Cheers!
Very nice article and I would like to see more of those as it explained very well an advanced topic of Flash. It would be nice to to give more examples of when these tips can be applied (in this one it only said to create dynamic APIs).
Great work
Thanks for the feedback zedia.net. I am already working on a few more of these style posts that will cover and advanced topic and try to boil it down into a 10 min explanation/demo. I would love to give more examples but really wanted to keep the entire reading short. Here is an example of how I have used rest in my own Flash Camo Framework.
In Camo I have a custom CSS parser. When I want to get a style I call getSelector and pass it in a style name. Well the getSelector method accepts a rest parameter. This allows me to pass in multiple selector (style) names and the parser automatically merges them into a single style based on the order you pass them in. First selector is lowest priority and later selector override the previous ones if they have over lapping properties. This is a great way to handle any number of selector names and not lock myself into say passing in an array as a fixed property of a method.
You can see this in action on line 178 here: http://code.google.com/p/flash-camouflage/source/browse/trunk/src/camo/core/property/PropertySheet.as
Hope this helps.
Just worth noting that if you ever override a method that utilizes the rest parameter, you have to use Function.apply() to call the super and pass the parameters on.
Nice, but I would say the idea of a series of articles like this has already been done: if you want a fuller list of AS3 trivia (i.e. how to deal with more AS3 needs) than you could imagine, see Senocular's "ActionScript 3 Tip of the Day" series over at http://www.kirupa.com/forum/showthread.php?t=223798.
Chock full of all the valuable bits of knowledge in AS3.
Just in case anyone ever comes across this issue of using the apply method on ExternalInterface.call() method:
public static function executeJavaScript(methodName:String, ...args):void { if (args.length == 0) { ExternalInterface.call(methodName); } else { args.splice(0, 0, methodName); ExternalInterface.call.apply(null, args); } }