其余参数

句法

```function f(a, b, ...theArgs) {
// ...
}
```

描述

```function myFun(a, b, ...manyMoreArgs) {
console.log("a", a)
console.log("b", b)
console.log("manyMoreArgs", manyMoreArgs)
}
myFun("one", "two", "three", "four", "five", "six")
// Console Output:
// a, one
// b, two
// manyMoreArgs, [three, four, five, six]
```

从自变量到数组

Rest parameters have been introduced to reduce the boilerplate code that was induced by the arguments

```// Before rest parameters, "arguments" could be converted to a normal array using:
function f(a, b) {
let normalArray = Array.prototype.slice.call(arguments)
// -- or --
let normalArray = [].slice.call(arguments)
// -- or --
let normalArray = Array.from(arguments)
let first = normalArray.shift()  // OK, gives the first argument
let first = arguments.shift()    // ERROR (arguments is not a normal array)
}
// Now, you can easily gain access to a normal array using a rest parameter
function f(...args) {
let normalArray = args
let first = normalArray.shift() // OK, gives the first argument
}
```

范例

使用其余参数

In this example, the first argument is mapped to ``` a ``` and the second to ``` b ``` , so these named arguments are used as normal.

However, the third argument, ``` manyMoreArgs ``` , will be an array that contains the 3 rd , 4 th , 5 th , 6 th ... n th — as many arguments that the user includes.

```function myFun(a, b, ...manyMoreArgs) {
console.log("a", a)
console.log("b", b)
console.log("manyMoreArgs", manyMoreArgs)
}
myFun("one", "two", "three", "four", "five", "six")
// a, one
// b, two
// manyMoreArgs, [three, four, five, six]
```

Below... even though there is just one value, the last argument still gets put into an array.

```// using the same function definition from example above
myFun("one", "two", "three")
// a, one
// b, two
// manyMoreArgs, [three]
```

Below, the third argument isn't provided, but ``` manyMoreArgs ``` is still an array (albeit an empty one).

```// using the same function definition from example above
myFun("one", "two")
// a, one
// b, two
// manyMoreArgs, []
```

自变量长度

Since ``` theArgs ``` is an array, a count of its elements is given by the ``` length ``` 特性：

```function fun1(...theArgs) {
console.log(theArgs.length)
}
fun1()         // 0
fun1(5)        // 1
fun1(5, 6, 7)  // 3
```

普通参数和其余参数

In the next example, a rest parameter is used to collect all parameters after the first into an array. Each one of them is then multiplied by the first parameter, and the array is returned:

```function multiply(multiplier, ...theArgs) {
return theArgs.map(element => {
return multiplier * element
})
}
let arr = multiply(2, 1, 2, 3)
console.log(arr)  // [2, 4, 6]
```

与自变量对象一起使用

``` Array ``` methods can be used on rest parameters, but not on the ``` arguments ``` 对象：

```function sortRestArgs(...theArgs) {
let sortedArgs = theArgs.sort()
return sortedArgs
}
console.log(sortRestArgs(5, 3, 7, 1)) // 1, 3, 5, 7
function sortArguments() {
let sortedArgs = arguments.sort()
return sortedArgs  // this will never happen
}
console.log(sortArguments(5, 3, 7, 1))
// throws a TypeError (arguments.sort is not a function)
```

```function sortArguments() {
let args = Array.from(arguments)
let sortedArgs = args.sort()
return sortedArgs
}
console.log(sortArguments(5, 3, 7, 1))  // 1, 3, 5, 7
```

浏览器兼容性

 Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome 47 Edge 12 Firefox 15 IE No Opera 34 Safari 10 WebView Android 47 Chrome Android 47 Firefox Android 15 Opera Android 34 Safari iOS 10 Samsung Internet Android 5.0 nodejs 6.0.0 6.0.0 4.0.0 Disabled Disabled From version 4.0.0: this feature is behind the ``` --harmony ``` runtime flag. Chrome 49 Edge 79 Firefox 52 IE No Opera 36 Safari 10 WebView Android 49 Chrome Android 49 Firefox Android 52 Opera Android 36 Safari iOS 10 Samsung Internet Android 5.0 nodejs Yes

另请参阅

1. JavaScript
2. 教程：
3. 完整初学者
4. JavaScript 指南
5. 中间体
6. 高级
7. 参考：
8. 内置对象
9. 表达式 & 运算符
10. 语句 & 声明
11. 函数
12. 错误
13. 杂项