Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arbitrary number of parameters in AS3

Does ActionScript 3.0 offer any means to accept an arbitrary number of parameters? I usually use .NET, but I'm being forced to use AS3 for a project and something along the lines of function blah(params double[] x) would be awesome for a helper library.

Thanks;

like image 830
hb. Avatar asked Feb 23 '09 22:02

hb.


3 Answers

Try an ellipse (like C) ...

function trace_all (... args): void {
    for each (a in args) {
       trace (a);
    }
}
like image 45
eduffy Avatar answered Nov 07 '22 06:11

eduffy


Check out the rest parameter: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#..._(rest)_parameter

package {
  import flash.display.MovieClip;

  public class RestParamExample extends MovieClip {
    public function RestParamExample() {
        traceParams(100, 130, "two"); // 100,130,two
        trace(average(4, 7, 13));     // 8
    }
  }
}


function traceParams(... rest) {
 trace(rest);
}

function average(... args) : Number{
  var sum:Number = 0;
  for (var i:uint = 0; i < args.length; i++) {
    sum += args[i];
  }
  return (sum / args.length);
}
like image 187
Christophe Herreman Avatar answered Nov 07 '22 07:11

Christophe Herreman


In addition to the "rest" parameter, there is an "arguments" object.

function foo() {
    for (var i:Number = 0; i < arguments.length; i++) {
        trace(arguments[i]);
    }
}
like image 2
Parappa Avatar answered Nov 07 '22 06:11

Parappa