填写(...休息)参数与数组?数组、参数

2023-09-08 11:08:43 作者:心疼那个笑着说不痛的自己

有些AS3函数处理通过允许使用约定的参数任意数量的超载

 公共职能DoSomething的(...休息):无效;
 

我是在一个情况下,我需要一个数组(任意长度)的所有值传递到这种​​类型的函数......我不知道如何做到这一点。建议?

下面是一个黑客攻击的解决方案,但它是不可扩展的:

 开关(args.length){
情况下0:DoSomething的();打破;
案例1:DoSomething的(参数[0]);打破;
案例2:DoSomething的(参数[0],的args [1]);打破;}
 
二维数组传递参数 二维数组 参数传递 CSDN

解决方案

查看Function#Apply().它可以让你通过参数数组。

  doSomething.apply(contextObj,参数);
 

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

解决方案

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

doSomething.apply(contextObj, args);