2007年5月25日星期五

Undetermined Number of Arguments With ...(rest)

Since ActionSript 3 checks argument count when functions are called you are not able to pass any number of arguments to any function like you could in ActionScript 1 or ActionScript 2. Instead, to allow for this, you need to use a new special kind of parameter called ...(rest) (Keyword: ...(rest)).

The ...(rest) parameter is a special parameter placed at the end of a parameter list in a function that specifies that there can be any number of additional arguments of any type passed into that function when its called. The form of the parameter is 3 periods followed by a keyword. When the function is called, the additional arguments are assigned to that keyword in the form of an array.

ActionScript Code:

function usingRest(required:Number, ... optionalArgs):void {
trace(required); // 1
trace(optionalArgs); // [2, 3, 4]
}
usingRest(
1, 2, 3, 4);

1评论:

Typhone 说...

似乎開玩笑開大了哈。

參數竟然可以不確定,那怎麼在內存裡尋址哦?搞不明白,總覺得太不安全了