Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filling in (...rest) parameters with an array?

Some as3 functions handle overloading by allowing for an arbitrary number of parameters using the convention:

public function doSomething( ... rest ):void;

I am in a situation where I need to pass all the values of an array (of arbitrary length) into this type of function... I am not sure how to do this. Suggestions?

Here is a hack solution, but it is not extensible:

switch (args.length) {
case 0: doSomething(); break;
case 1: doSomething(args[0]); break;
case 2: doSomething(args[0], args[1]); break;}
like image 683
jedierikb Avatar asked Mar 12 '09 00:03

jedierikb


2 Answers

Check out Function#Apply(). It lets you pass the parameters as an array.

doSomething.apply(contextObj, args);
like image 119
Chetan S Avatar answered Nov 06 '22 17:11

Chetan S


Here is a very good tip to pass the rest parameter between functions.

like image 36
LE GALL Benoît Avatar answered Nov 06 '22 19:11

LE GALL Benoît