freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../es6/use-the-rest-operator-with-.../index.md

1.3 KiB
Raw Blame History

title localeTitle
Use the Rest Operator with Function Parameters 将Rest运算符与函数参数一起使用

将Rest运算符与函数参数一起使用

休息参数说明

Mozilla开发者网络

与rest参数相比Spread运算符

堆栈溢出

视频解释传播和休息

“youtube视频链接传播和休息运算符的图像”width

[### 例

这段代码

const product = (function() { 
    "use strict"; 
    return function product(n1, n2, n3) { 
        const args = [n1, n2, n3]; 
        return args.reduce((a, b) => a * b, 1); 
    }; 
 })(); 
 console.log(product(2, 4, 6));//48 

可以这样写

const product = (function() { 
    "use strict"; 
    return function product(...n) { 
        return n.reduce((a, b) => a * b, 1); 
    }; 
 })(); 
 console.log(product(2, 4, 6));//48 

```](http://www.youtube.com/watch?feature=player_embedded&v=iLx4ma8ZqvQ
)