filling in (…rest) parameters with an array?

廉价感情. 提交于 2019-12-01 00:08:19

问题


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;}

回答1:


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

doSomething.apply(contextObj, args);



回答2:


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



来源:https://stackoverflow.com/questions/636853/filling-in-rest-parameters-with-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!