rest 参数

形式为...变量名

一个不定数量的参数表示为一个数组

   function sum(...theArgs) {
            return theArgs.reduce((previous, current) => {
                return previous + current;
            });
        }

   console.log(sum(1, 2, 3)); // 6

  

function f(...[a, b, c]) {
  return a + b + c;
}

f(1)          // NaN (b and c are undefined)
f(1, 2, 3)    // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not destructured)

  

posted @ 2020-03-24 15:07  banzhuxiang  阅读(183)  评论(0)    收藏  举报