我想知道是否有某種方法來解壓AS3中的變長參數列表。就拿這個功能:展開AS3中的變量參數
public function varArgsFunc(amount:int, ...args):Array
{
if (amount == 3)
{
return args
}
else
{
return varArgsFunc(++amount, args)
}
}
如果我把這叫做:
var result:Array = varArgsFunc(0, [])
結果現在包括一組嵌套的數組:
[[[[]]]]
這裏的問題是,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存在擴大的技術/運營商一個數組放入參數列表中。
感謝您的迴應,但這不是我正在尋找對。我已經重寫了這個問題,試圖更清楚地解釋我自己。 – 2009-09-10 14:56:43
hokay,編輯我的答案。現在好多了? ;) – grapefrukt 2009-09-10 19:52:21
感謝葡萄柚 – 2009-09-11 07:40:59