arguments對象的0...n屬性用于返回調用當前函數所傳入的參數。訪問arguments的這些屬性類似于訪問數組中的元素。
該屬性屬于arguments對象,每個函數在被調用時,都有一個內置的arguments對象。所有主流瀏覽器均支持該屬性。
## 語法
```
[functionObject.]arguments[ n ]
```
## 參數
| 參數 | 描述 |
| --- | --- |
| n | Number類型傳入參數的順序索引,第一個參數的索引為0。 |
參數n應該是在 0 到 arguments.length-1 范圍內的整數。其中,0表示傳入的第一個參數,1表示傳入的第二個參數……arguments.length-1表示傳入的最后一個參數。如果參數n超出該范圍,將返回undefined。
## 返回值
0...n的值為任意類型,返回調用當前函數所傳入的第n + 1個參數。
由0...n屬性返回的值就是傳遞給正在執行的當前函數的參數值。雖然arguments對象不是數組,但訪問組成arguments對象的各個元素的方法與訪問數組元素的方法相同。
## 示例&說明
```
function test(){
// 循環輸出調用當前函數時傳入的參數
for(var n = 0; n < arguments.length; n++){
document.write( arguments[n] + "|");
}
document.write('<br>'); // 輸出換行符進行區分
};
test(); // (空字符串)
test("Code", "365mini.com", 12, true); // Code|365mini.com|12|true|
test("張三", false, 12.315); // 張三|false|12.315|
function foo(){
// arguments前也可以加上函數名稱
for(var n = 0; n < foo.arguments.length; n++){
document.write( foo.arguments[n] + "|");
}
document.write('<br>');
};
foo("Hello", "World"); // Hello|World|
// 使用arguments對象實現任意多個數的求和
function sum(){
var sum = arguments[0];
for(var n = 1; n < arguments.length; n++){
sum += arguments[n];
}
return sum;
}
document.writeln( sum() ); // undefined
document.writeln( sum( 1, 5 ) ); // 6
document.writeln( sum( 1, 212, 21, -14, 23 ) ); // 243
```