扩展运算符和rest参数

115 阅读1分钟

扩展运算符: 把数组或者类数组展开成用逗号分割的值

rest参数: 把逗号分割开的值组合成一个数组

扩展运算符例子

   //扩展运算符
    let arr = [1, 2, 3]
    console.log(...arr); //1 2 3

    function a(x, y, z) {
      console.log(x, y, z);//1 2 3
    }
    a(...arr)

rest参数例子

   //rest参数
    function b(...rest) {
      console.log(rest);
    }
    b(1, 2) //[1, 2]
    b(1, 2, 3) // [1, 2, 3]