length 屬性 (引數)
更新:2007 年 11 月
傳回由呼叫端傳給函式的實際引數個數。
[function.]arguments.length
引數
- function
選擇項。目前正在執行的 Function 物件名稱。
備註
指令碼引擎會在開始執行函式時,將 arguments 物件的 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