`arguments`對象是所有(非箭頭)函數中都可用的**局部變量**。你可以使用`arguments`對象在函數中引用函數的參數。此對象包含傳遞給函數的每個參數,第一個參數在索引0處。例如,如果一個函數傳遞了三個參數,你可以以如下方式引用他們:
~~~js
arguments[0]
arguments[1]
arguments[2]
~~~
`arguments`對象不是一個[`Array`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Array "entries() 方法返回一個新的Array Iterator對象,該對象包含數組中每個索引的鍵/值對。")?。它類似于`Array`,但除了length屬性和索引元素之外沒有任何`Array`屬性。例如,它沒有?[pop](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/pop "JavaScript/Reference/Global_Objects/Array/pop")?方法。但是它可以被轉換為一個真正的`Array`
~~~js
var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);
// ES2015
const args = Array.from(arguments);
const args = [...arguments];
~~~
定義鏈接字符串函數
~~~js
function myConcat(separator) {
var args = Array.prototype.slice.call(arguments, 1);
return args.join(separator);
}
~~~
~~~js
// returns "red, orange, blue"
myConcat(", ", "red", "orange", "blue");
~~~
: