填写(...休息)参数与数组? [英] filling in (...rest) parameters with an array?

查看:88
本文介绍了填写(...休息)参数与数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

解决方案

查看<一个href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Function.html#apply%28%29">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);

这篇关于填写(...休息)参数与数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆