views:

316

answers:

2

I need to call a varargs function:

function doSomething(... args): Object {
    // do something with each arg
}

However, I'm building the arguments for this dynamically:

var someArgs: Array = ['a', 'b', 'c'];
doSomething(someArgs);

The problem is, when I call the function in this way args ends up being a 1-element array with someArgs as the first element, not a three-element array.

How can I call doSomething with someArgs as the argument array?

(For the search engines, this is argument unpacking)

+5  A: 

Use Function.apply.

Like this:

doSomething.apply(null, someArgs);

If doSomething is a method of a class, pass in the class instead of null.

Brian
actually, it does not matter, whether you pass in the class/instance ... AS3 automatically creates method closures, where "this" is preassigned to always be the owner of the method ...
back2dos
I ended up finding this about ten minutes after posting. I figured I'd still provide rep to whomever answered, and it's nice to have it on SO.com.
Chris R
A: 

Hi!

I think that the following will do the trick :)

function doSomething(...):Object
{
 // do something with each arg
}
Adrian Pirvulescu