AS3: Default parameter value


In ActionScript 3 it’s possible to automatically assign a default value to parameters in a function.

Parameters with optional value should be placed at the end of the function parameter list, while all the required ones at the begining.

Parameters with default values can be ommitted. This gives us the following possibilities:

function twoPlusTwoIs (result:int = 2) {

trace( “Result: ” + result );

}

twoPlusTwoIs();    —>  2

twoPlusTwoIs(3);  —>  3

Likewise, a more plausable usage:

function enable ( val:Boolean = true ) {

someVariable = val;

}

MyFancyObject.enable();           —>  true

MyFancyObject.enable(true);     —> true

MyFancyObject.enable(false);     —> false

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s