Blogadda

Blogadda
Visit BlogAdda.com to discover Indian blogs

Monday, August 3, 2009

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.

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

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.


http://www.linkedin.com/news?viewArticle=&articleID=55481685&gid=
113435&articleURL=http%3A%2F%2Fwww.insideria.com%2F2009%2F08%2Fas-3-
rest-parameter.html&urlhash=9xja&trk=news_discuss

No comments: