ES5利用arguments
// ES5 function sum () { let num = 0 Array.prototype.forEach.call(arguments, function (v) { num += v * 1 }) return num } console.log(sum(1, 2, 3, 4, 5)) 15
ES6不推荐arguments,用扩展符
// ES6 let sum = (...arr) => { let num = 0 arr.forEach(v => { num += v * 1 }) return num } console.log(sum(1, 2, 3, 4, 5))
15