在方法中使arguments数组,一般情况下,方法需要的参数都是在参数定义时写出的,但是有些特定的情况下,参数的个数是未知的,在这种情况下,就可以使用flash内建的arguments数组,所以向方法传递的参数都被自动的放入arguments数组中
例:// There is no need to specify the parameters to accept when using the
// arguments array.
private function average( ):Number {
var sum:Number = 0;
// Loop through each of the elements of the arguments array, and
// add that value to sum.
for (var i:int = 0; i < arguments.length; i++) {
sum += arguments[i];
}
// Then divide by the total number of arguments
return sum/arguments.length;
}
// You can invoke average( ) with any number of parameters.
var average:Number = average (1, 2, 5, 10, 8, 20);
private function average( ):Number {
var sum:Number = 0;
// Loop through each of the elements of the arguments array, and
// add that value to sum.
for (var i:int = 0; i < arguments.length; i++) {
sum += arguments[i];
}
// Then divide by the total number of arguments
return sum/arguments.length;
}
// You can invoke average( ) with any number of parameters.
var average:Number = average (1, 2, 5, 10, 8, 20);