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)
Use Function.apply.
Like this:
doSomething.apply(null, someArgs);
If doSomething is a method of a class, pass in the class instead of null.
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