• ES6中的(...)运算符详细学习


    1.1 合并数组

    1. //es5写法,使用concat
    2. let arr1 = [1,2];
    3. let arr2 = [5,6];
    4. let newArr = [20];
    5. newArr = newArr.concat(arr1).concat(arr2);
    6. //es6写法
    7. let arr1 = [1,2];
    8. let arr2 = [5,6];
    9. let newArr = [20];
    10. newArr = [20,...arr1,...arr2];

     1.2合并对象

    1. const baseSquirtle = {
    2. name: 'Squirtle',
    3. type: 'Water'
    4. };
    5. const squirtleDetails = {
    6. species: 'Tiny Turtle Pokemon',
    7. evolution: 'Wartortle'
    8. };
    9. const squirtle = { ...baseSquirtle, ...squirtleDetails };
    10. console.log(squirtle);
    11. //Result: { name: 'Squirtle', type: 'Water', species: 'Tiny Turtle Pokemon', evolution: 'Wartortle' }

    2.1 为数组新增成员

    1. const pokemon = ['小红', '小李'];
    2. const charmander = '大大';
    3. const pokedex = [...pokemon, charmander];
    4. console.log(pokedex);
    5. //Result: [ '小红', '小李', '大大' ]

     2.2 为对象新增属性

    1. const aa= { name: '小红', type: '123' };
    2. const obj= {
    3. ...aa,
    4. unit: '米',
    5. id: '1'
    6. };
    7. console.log(obj);
    8. //Result: { name: '小红', type: '123', unit: '米', id: '1' }
    • 将一个数组添加到另一个数组的尾部:
      1. let arr1 = [0, 1, 2];
      2. let arr2 = [3, 4, 5];
      3. //es5写法
      4. Array.prototype.push.apply(arr1, arr2);
      5. //es6写法
      6. let arr1 = [0, 1, 2];
      7. let arr2 = [3, 4, 5];
      8. arr1.push(...arr2);
    • 将字符串转换成数组:
      1. //es5写法需要split和join的操作
      2. //...
      3. //es6写法
      4. [...'hello']
      5. // [ "h", "e", "l", "l", "o" ]

      解构赋值

    • 例1:

      1. let obj = {name:"小明",age:18,hobby:"小红"};
      2. let newobj = {
      3. ...obj
      4. }
      5. console.log(newobj)//和obj一样

      例2:

      1. let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
      2. console.log(x); // 1
      3. console.log(y); // 2
      4. console.log(z); // { a: 3, b: 4 }

      可以对数组进行浅克

      1. let arr = [1,2,[1,2],3];
      2. let arr2 = [...arr];
      3. arr2.push(1);
      4. console.log(arr);//[1,2,[1,2],3]
      5. console.log(arr2);//[1,2,[1,2],3,1]

     进阶:

    1. 复制具有嵌套结构的数据/对象

    先看一个例子:

    1. const pokemon = {
    2. name: 'Squirtle',
    3. type: 'Water',
    4. abilities: ['Torrent', 'Rain Dish']
    5. };
    6. const squirtleClone = { ...pokemon };
    7. pokemon.name = 'Charmander';
    8. pokemon.abilities.push('Surf');
    9. console.log(squirtleClone);
    10. //Result: { name: 'Squirtle', type: 'Water', abilities: [ 'Torrent', 'Rain Dish', 'Surf' ] }

    当我们修改原对象的name 属性时,我们的克隆对象的 name 属性没有受影响, 这是符合我们预期的。但是当修改原对象的abilities 属性时,我们的克隆对象也被修改了

    原因: 因为复制过来的abilities 是一个引用类型, 原数据改了, 用到他的地方也会跟着改

    解决办法:const squirtleClone = { ...pokemon, abilities: [...pokemon.abilities] };

    1. const pokemon = {
    2. name: 'Squirtle',
    3. type: 'Water',
    4. abilities: ['Torrent', 'Rain Dish']
    5. };
    6. const squirtleClone = { ...pokemon, abilities: [...pokemon.abilities] };
    7. pokemon.name = 'Charmander';
    8. pokemon.abilities.push('Surf');
    9. console.log(squirtleClone);
    10. //Result: { name: 'Squirtle', type: 'Water', abilities: [ 'Torrent', 'Rain Dish' ] }

    增加条件属性

    方式一:

    1. const pokemon = {
    2. name: 'Squirtle',
    3. type: 'Water'
    4. };
    5. const abilities = ['Torrent', 'Rain dish'];
    6. const fullPokemon = abilities ? { ...pokemon, abilities } : pokemon;
    7. console.log(fullPokemon);

    方式二:简化一下

    const fullPokemon = abilities && { ...pokemon, abilities };

    短路

    1. const pokemon = {
    2. name: 'Squirtle',
    3. type: 'Water'
    4. };
    5. const abilities = ['Torrent', 'Rain dish'];
    6. const fullPokemon = {
    7. ...pokemon,
    8. ...(abilities && { abilities })
    9. };
    10. console.log(fullPokemon);

    如果 abilities 为 true, 就相当于是

    1. const fullPokemon = {
    2. ...pokemon,
    3. ...{ abilities }
    4. }

  • 相关阅读:
    day09扩展:键盘录入笔记
    G-TAD: Sub-Graph Localization for Temporal Action Detection 论文阅读笔记
    【QCustomPlot】下载
    Tomcat老是卡住,关不掉怎么办?
    Web基础与HTTP协议
    【GO】基础速成
    大厂常问到的14个Java面试题
    案例精选|聚铭网络多产品联合部署为北京迎祥酒店建立信息安全屏障
    Element常用api webview
    Elasticsearch 进阶
  • 原文地址:https://blog.csdn.net/weixin_43923808/article/details/126667950