length 属性(参数)
更新:2007 年 11 月
返回由调用者传递给一个函数的实际参数数目。
[function.]arguments.length
参数
- function
可选项。当前正在执行的 Function 对象的名称。
备注
对象的 length 属性被脚本引擎初始化为该函数中开始执行时,传递给 Function 对象的实际参数数目。
示例
下面的示例阐释了 arguments 对象的 length 属性的用法。
function argTest(a, b) : String {
var i : int;
var s : String = "The argTest function expected ";
var numargs : int = arguments.length; // Get number of arguments passed.
var expargs : int = argTest.length; // Get number of arguments expected.
if (expargs < 2)
s += expargs + " argument. ";
else
s += expargs + " arguments. ";
if (numargs < 2)
s += numargs + " was passed.";
else
s += numargs + " were passed.";
s += "\n"
for (i =0 ; i < numargs; i++){ // Get argument contents.
s += " Arg " + i + " = " + arguments[i] + "\n";
}
return(s); // Return list of arguments.
}
print(argTest(42));
print(argTest(new Date(1999,8,7),"Sam",Math.PI));
当使用 /fast- 选项编译后,该程序的输出为:
The argTest function expected 2 arguments. 1 was passed.
Arg 0 = 42
The argTest function expected 2 arguments. 3 were passed.
Arg 0 = Tue Sep 7 00:00:00 PDT 1999
Arg 1 = Sam
Arg 2 = 3.141592653589793