rest 剩余参数

  • ES6 引入 rest 参数,用于获取函数的实参,用来替代 argument。

    1. ES5 获取形参方式

      function fn() {
          console.log(arguments);
      }
      
    2. ES6 获取形参方式

      let fn = (...args) => {
          console.log(args); // 数组
      }
      function fn(...args) {
          console.log(args)
      }
      fn(1, 2, 3, 4)
      
  • rest 参数必须放到参数的最后。

function fn(a, b, ...args) {
    console.log(a); //1
    console.log(b); //2
    console.log(args) // 3 4 5 6
}
fn(1, 2, 3, 4, 5, 6)
posted @ 2022-02-16 23:40  HuangBingQuan  阅读(58)  评论(0编辑  收藏  举报