ES6 推出了用于函数参数的 rest 操作符帮助我们创建更加灵活的函数。 rest 操作符可以用于创建有一个变量来接受多个参数的函数。 这些参数被储存在一个可以在函数内部读取的数组中。
请看以下代码:
function howMany(…args) {
return “You have passed " + args.length + " arguments.”;
}
console.log(howMany(0, 1, 2));
console.log(howMany(“string”, null, [1, 2, 3], { }));
控制台将显示字符串 You have passed 3 arguments. 和 You have passed 4 arguments.。
使用 rest 参数,就不需要查看 args 数组,并且允许我们在参数数组上使用 map()、filter() 和 reduce()
题目:修改 sum 函数,使用 rest 参数,使 sum 函数可以接收任意数量的参数,并返回它们的总和。
//题目代码
// const sum = (x, y, z) => {
// const args = [x, y, z];
// return args.reduce((a, b) => a + b, 0);
// }
//freecodecamp 参考代码
const sum = (...args) => {
return args.reduce((a, b) => a + b, 0);
}