OK, so we all know that using the ... (rest) parameter in a function lets us pass any number of arguments, as in the example in the Adobe AS3 manual:
function traceArgArray(x: int, ... args) {
for (var i:uint = 0; i < args.length; i++) {
trace(args[i]);
}
}
traceArgArray(1, 2, 3);
BUT, what I would love to be able to do is to have the option of EITHER passing individual arguments OR passing arguments from an already-existing array:
myArray:Array = new Array(1,2,3);
and so:
traceArgArray(myArray);
As I've written it here, the function treats myArray as a single object, so the output is 1,2,3--a representation of the array as a whole, not the individual contents of the array.
In order to transform myArray into a comma-delimited list that the rest operator would recognize as a list of individual elements, I tried this:
traceArgArray(myArray.join());
but that didn't change the output. It looks like in this case the argument is being interpreted as a single string rather than a comma-delimited list.
So, the puzzle I hope someone can help me with can be expressed in either of two ways:
Is it possible to get the rest parameter to treat an array argument as a comma-delimited list of arguments?
-or-
Is there a way I can transform an array into a comma-delimited list and then pass that list to the function AS a comma-delimited list (rather than a long string) so that the rest operator can interpret it correctly?
The basic thing I want to do--be able to pass arrays of values into functions that accept any number of arguments--would seem to be of general use, so perhaps there is a work-around I'm not seeing because I'm obsessing over the rest operator itself.
Thanks for reading!