用数组填充 (...rest) 参数? [英] filling in (...rest) parameters with an array?

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

问题描述

一些 as3 函数通过使用约定允许任意数量的参数来处理重载:

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

推荐答案

查看 函数#Apply().它允许您将参数作为数组传递.

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

doSomething.apply(contextObj, args);

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

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