• es6 扩展运算符三个点(...)的用法及总结


    不明白的:

    1.0 apply()

    2.0 合并数组的几种方法(Array.prototype.push.apply(arr1,arr2))//不理解

    3.0 Iterator 接口的对象

    4.0 Map 和 Set 结构, Generator 函数

    1.0 含义 扩展运算符(spread)是... , 将一个数组转为逗号分隔的参数序列

    console.log(...[1,2,3])//1 2 3
    console.log(1,...[2,3],4)//1 2 3 4
    2.0 该运算符主要用于函数调用。

    function push(array, ...items) {array.push(...items); }

    function add(x, y) {return x + y;}

    var numbers = [4, 38];

    add(...numbers) // 42

      上面代码中,array.push(...items)和add(...numbers)这两行,都是函数的调用,它们的

    使用扩展运算符,该运算符讲一个数据,变为参数序列。

    3.0 扩展运算符与正常函数参数可以结合使用。风场灵活。

    function f(v, w, x, y, z) { }

    var args = [0, 1];

    f(-1, ...args, 2, ...[3]);

    2  替代数组的 apply 方法

    由于扩展运算符可以展开数组,所以不再需要apply方法,将数组转为函数的参数了。

    1.  
      // ES5 的写法
    2.  
      function f(x, y, z) {
    3.  
      // ...
    4.  
      }
    5.  
      var args = [0, 1, 2];
    6.  
      f.apply(null, args);
    7.  
      // ES6 的写法
    8.  
      function f(x, y, z) {
    9.  
      // ...
    10.  
      }
    11.  
      var args = [0, 1, 2];
    12.  
      f(...args);


    下面是扩展运算符取代apply方法的一个实际的例子,应用Math.max方法,简化求出一个数组最大元素的写法。

    console.log(Math.max.apply(null,[1,3,8]));
    console.log(Math.max(...[1,3,8]))
    等同于 Math.max(1,3,8)

     上面代码表示,由于 JavaScript 不提供求数组最大元素的函数,所以只能套用Math.max函数,将数组转为一个参数序列,然后求最大值。有了扩展运算符以后,就可以直接用Math.max了。

    另一个例子是通过push函数,将一个数组添加到另一个数组的尾部。


    let a = [1,2,3];
    let b = [1,2,3];
    Array.prototype.push.apply(a,b);
    console.log(a);//[1, 2, 3, 1, 2, 3]
    console.log(b);//[1, 2, 3]


    let a = [1,2,3];
    let b = [1,2,3];

    a.push(...b);
    console.log(a);//[1, 2, 3, 1, 2, 3]
    console.log(b)

    上面代码的 ES5 写法中,push方法的参数不能是数组,所以只好通过apply方法变通使用push方法。有了扩展运算符,就可以直接将数组传入push方法。
    下面是另外一个例子。

    1. console.log(new (Date.bind.apply(Date, [null, 2015, 1, 1])));//[1, 2, 3, 1, 2, 3]
      console.log(new Date(...[2015, 1, 1])) //Sun Feb 01 2015 00:00:00 GMT+0800

    3  扩展运算符的应用

    ( 1 )合并数组

    扩展运算符提供了数组合并的新写法。

    let a = ['a','b'];
    let b = ['c'];
    let c = ['b','e']
    let d= a.concat(c,b);
    console.log(a);//["a", "b"]
    console.log(d);//["a", "b", "c", "b", "e"]
    console.log([...a,...b,...c])//["a", "b", "c", "b", "e"]

    ( 2 )与解构赋值结合

    扩展运算符可以与解构赋值结合起来,用于生成数组。

    const [a,...b] = [1,2,3]
    console.log(a);//1
    console.log(b)//[2,3]
    const [one,...two] = []
    console.log(one)//undefined
    console.log(two)//[]
    const [first,...rest] = ["foo"];
    console.log(first)//foo
    console.log(rest)//[]

    如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错。

    const [...butLast, last] = [1, 2, 3, 4, 5];
    // Uncaught SyntaxError: Rest element must be last element
    const [first, ...middle, last] = [1, 2, 3, 4, 5];

    ( 4 )字符串

    扩展运算符还可以将字符串转为真正的数组。

    console.log(..."hello")//h e l l o
    console.log([..."hello"]);//["h", "e", "l", "l", "o"]

    上面的写法,有一个重要的好处,那就是能够正确识别 32 位的 Unicode 字符。

    ( 5 )实现了 Iterator 接口的对象

    任何 Iterator 接口的对象,都可以用扩展运算符转为真正的数组。

    1.  
      var nodeList = document.querySelectorAll('div');
    2.  
      var array = [...nodeList];

    上面代码中,querySelectorAll方法返回的是一个nodeList对象。它不是数组,而是一个类似数组的对象。这时,扩展运算符可以将其转为真正的数组,原因就在于NodeList对象实现了 Iterator 接口。

    对于那些没有部署 Iterator 接口的类似数组的对象,扩展运算符就无法将其转为真正的数组。

    let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
    };
    // TypeError: arrayLike is not iterable
    let arr = [...arrayLike];
    console.log(arr)

    上面代码中,arrayLike是一个类似数组的对象,但是没有部署 Iterator 接口,扩展运算符就会报错。这时,可以改为使用Array.from方法将arrayLike转为真正的数组。

    ( 6 ) Map 和 Set 结构, Generator 函数

    扩展运算符内部调用的是数据结构的 Iterator 接口,因此只要具有 Iterator 接口的对象,都可以使用扩展运算符,比如 Map 结构。

    1.  
      let map = new Map([
    2.  
      [1, 'one'],
    3.  
      [2, 'two'],
    4.  
      [3, 'three'],
    5.  
      ]);
    6.  
      let arr = [...map.keys()]; // [1, 2, 3]

    Generator 函数运行后,返回一个遍历器对象,因此也可以使用扩展运算符。

    1.  
      var go = function*(){
    2.  
      yield 1;
    3.  
      yield 2;
    4.  
      yield 3;
    5.  
      };
    6.  
      [...go()] // [1, 2, 3]

    上面代码中,变量go是一个 Generator 函数,执行后返回的是一个遍历器对象,对这个遍历器对象执行扩展运算符,就会将内部遍历得到的值,转为一个数组。
    如果对没有iterator接口的对象,使用扩展运算符,将会报错。

      1.  
        var obj = {a: 1, b: 2};
      2.  
        let arr = [...obj]; // TypeError: Cannot spread non-iterable object
  • 相关阅读:
    模板方法模式
    组合模式
    JS API文档
    支持 @connect写法
    PHP word导入题库
    go 复制文件和创建目录
    go文件写入
    go 文件读取
    gin连接mysql数据库
    gin多数据格式返回结果
  • 原文地址:https://www.cnblogs.com/lieaiwen/p/10254774.html
Copyright © 2020-2023  润新知