javascript ...休息参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ...休息参数相关的知识,希望对你有一定的参考价值。

// Gather the remaining parameters into an array
// rest params must be at the end
const func = (a, b, ...args) => {
  console.log(args); // [4, 6, 7]
  var newArgs = args.reduce((arg, currVal) => {
    return arg * currVal;
  })
  return newArgs;
}
func(1, 2, 4, 6, 7); // 168

// arguments object
// Gives us the number of arguments passed in the function
// Looks like an array, but not actually an array
// the prototype is an object
// It's a list with one mthod on it, .length
// However, you can iterate over it with a 'for of' loop because it has a 'symbol.iterator'
// Does not support array methods
// arrow functions do not have 'arguments'
function addUpNumbers() {
    let total = 0;
    for (const num of arguments) {
      console.log(arguments.length);
      total += num;
    }
    console.log(total);
    return total;
  }

  addUpNumbers(10,23,52,34,12,13,123);

以上是关于javascript ...休息参数的主要内容,如果未能解决你的问题,请参考以下文章

javascript ES6 - 休息参数

JavaScript ES6 - 这是扩展语法还是休息语法?

javascript Api网络服务休息

javascript 休息属性

javascript 传播和休息运营商

javascript 基本休息为express.js创建功能