AS3: Notes on arguments…
I keep forgetting how to handle arguments in AS3. If you haven’t started to migrate yet, you should be aware that handling arguments is among the more frustrating aspects. Arguments are much stricter in AS3, which is a blessing and a curse. A blessing because you have far more control over what a method will accept (and as a result smarter, cleaner code). And a curse because… well, I already said that.
Here are a couple of quick hints on arguments that I find I need to keep reminding myself about:
overloading
If you need a function to handle a variable number of arguments, use a rest (…).:
public function onFoo(... fooArgs):void
{
for(var i:int=0;i<fooArgs.length;i++) { trace(fooArgs[i]); }
};
Here’s some more on overloading.
default variables
AS3 will throw an error if you attempt to pass null for an argument where it expects one. That is, unless you define a default variable.
public function onFoo($myNumber:Number = 1):void
{
};
or this example, while would let you call the method an event would normally fire without the event.
public function onSomeEvent($evt:Event = null):void
{
};
*
* lets you define anything as the DataType for a variable. Handy.
public function onFoo($myArg:*):void
{
};


