我想知道是否有某种方法可以在 AS3 中解压缩可变长度参数列表。以这个函数为例:
public function varArgsFunc(amount:int, ...args):Array
{
if (amount == 3)
{
return args
}
else
{
return varArgsFunc(++amount, args)
}
}
如果我这样称呼:
var result:Array = varArgsFunc(0, [])
result 现在包含一组嵌套的数组:
[[[[]]]]
这里的问题是 args 参数被视为一个数组。因此,如果我将它传递给具有可变参数列表的函数,它将被视为单个参数。
在 Scala 中有一个 :_* 运算符,它告诉编译器将一个列表分解为一个参数列表:
var list:Array = ['A', 'B', 'C']
// now imagine we have this class, but I would like to pass each list element
// in as a separate argument
class Tuple {
public function Tuple(...elements)
{
// code
}
}
// if you do this, the list will become be passed as the first argument
new Tuple(list)
// in scala you can use the :_* operator to expand the list
new Tuple(list :_*)
// so the :_* operator essentially does this
new Tuple(list[0], list[1], list[2])
我想知道 AS3 中是否存在将数组扩展为参数列表的技术/运算符。