Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call a varargs function with an array in ActionScript?

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)

like image 614
Chris R Avatar asked Aug 11 '09 18:08

Chris R


1 Answers

Use Function.apply.

Like this:

doSomething.apply(null, someArgs);

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

like image 125
Brian Avatar answered Sep 19 '22 16:09

Brian