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;
Try an ellipse (like C) ...
function trace_all (... args): void {
for each (a in args) {
trace (a);
}
}
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);
}
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]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With