Beginning ActionScript 3.0 – More About Parameters

a-team-van-256x256 Besides the default parameters, there are still some thing you need to know about the parameters in ActionScript 3.0, the Arguments object and the …(rest) parameter.

Arguments object

In last section, we said that when calling the functions we should use the same number of parameters with our function declaration. Of course, this is true, but it’s only for the compiler in strict mode. In fact, you can pass more parameters than the defined parameters, if you want to do this, you should use the compiler mode into standard mode.

If you’re using flash cs3, you can uncheck the strict mode to use the standard mode.

clip_image002

Now, take a look at the following code.

function argumentObjectTest(someValue:int):void {
for(var i:int = 0; i < arguments.length; i++)
trace(arguments[i]);
trace(arguments.callee.toString());
}

argumentObjectTest(1,2,3);

In the above code, we define a function with one formal parameter, while in the main program we call the function and pass three parameters. And the result will be:

clip_image004

So, from the result, you may know that actually we can get all the parameters passed into the function by using the argument object.

When parameters are passed to a function, you can use the arguments object to access information about the parameters passed to your function. The arguments object is an array that includes all the parameters passed to the function. The arguments.length property reports the number of parameters passed to the function. The arguments.callee property provides a reference to the function itself, which is useful for recursive calls to, and this property will be discussed with the recursion.

Note: The arguments object is not available if any parameter is named arguments or if you use the … (rest)

If you don’t need the callee property of arguments object, you can use another parameter, the … (rest) parameter, instead of using the arguments object.

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.

A short example is as follows:

function restParameterTest(…args):void {
for(var i:int = 0; i < args.length; i++)
trace(args[i]);
}

restParameterTest(1,2,3);

and the output is:

clip_image006

As you see, we use args to name the parameter here; in fact, the parameter can have any name that is not a reserved word.

If you want to use some fixed parameters with the rest parameters, you should place the rest parameter declaration at last. And the fixed parameters will not be included in the rest parameter array.

Change the code into follows:

function restParameterTest(someValue:int, …args):void {
for(var i:int = 0; i < args.length; i++)
trace(args[i]);
}

restParameterTest(1,2,3);

You can see the output will be:

clip_image008

When you use this parameter will make the arguments object unavailable.

function restParameterTest(someValue:int, …args):void {
for(var i:int = 0; i < arguments.length; i++)
trace(arguments[i]);
}

restParameterTest(1,2,3);

Try to compile this code; you will get an error like this:

clip_image010

So, you can’t use the rest parameter with the arguments object.

My suggestion is, use the rest parameter instead of the arguments object if you don’t need the callee property, because you can use the rest parameter not only in the standard mode, but also in the strict mode.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply