Default and Additional Method Parameters in AS 3.0

Something i covered briefly in my presentation the other day on AS 3.0 (the files are coming guys).

Default
You will get an error in AS 3.0 if a method doesn’t get passed the correct amount of paremeters i.e. if it expects 2 and you pass one you will get an error. This is a nice addition, but you can get round this using default values for parameters. If the correct amount of parameters are then not passed the method will use the default values instead of generating an error therefore making the parameters optional.

[as]private function testDefaultParams(noDefault:String, hasDefault:String = “Tink”):Void
{
trace(hasDefault + ” ” + noDefault);
}

testDefaultParams(“has a beard”) // Tink has a beard[/as]
Note that once you have specified a default parameter, all following parameters in that method also need a default value.

Additional
AS 3.0 also gives use the ability to pass more arguments to a method that it expects by using the … keyword[as]private function testExtraParams(m:String, …n):Void
{
trace(m);

for each (var s:String in n)
{
trace(s);
}
}

testExtraParams(“Tink”, “has”, “a”, “beard”);
//Tink
//has
//a
//beard
[/as]
So in the above example the first parameter is manitory. If it isn’t passed they you will get an error, but the second paremeter is optional and you can pass more arguments and access their values inside the method as an Array.

6 Responses to “Default and Additional Method Parameters in AS 3.0”

  1. veejay says:

    Great introduction to AS3! I am still waiting got those files though. This was my first time to MMUG and I am kicking myself not have attended any earlier.

    Thought I would add-in to your discovery. The optional parameter which has the default value cannot come before the required parameters. Since “n” is an Array, to trace its values without using the for..each loop you can call “trace(n)”.

    cheers

  2. [...] ith Parameters If you didn’t see it, Tink posted a nice little overview of a couple things you can do with function parameters in AS3. I knew about default para [...]

  3. Ludwig Van says:

    May I say this?

    STOP PUTTING AS CODE IN THOSE FUCKING SCROLLING TEXT BOXES.

    DO YOU THINK THAT IS EASY TO READ?

    ITS SHIT. WAKE THE FUCK UP.

  4. Ludwig Van says:

    I would also like to add: YOU CANT CUT AND PASTE THAT CODE BECAUSE OF THE LINE NUMBERS.

  5. Tink says:

    Obviously Ludwij you can say what you want, but you might get a reply such as…

    FUCK OFF!

    The line numbers are useful for when talking about particular lines of code and if you click the ‘PLAIN TEXT’ button u can copy and paste the code without the line numbers.

    DOH!!!

    Unfortunately for you, I wont be changing it :( . Its not ideal but better than no syntax highlighting at all.

  6. Will says:

    Hi Tink, Great stuff. I came across this post when I was searching for Actionscript 3.0 Enumerations, its probablly none of my bussines but I think you should remove Ludwig Van Post and yours as well. you should keep your blog clean from this rubbish.

    Regards.

Leave a Reply