zoukankan      html  css  js  c++  java
  • ES6学习及总结(二):对象的解构

    ES6学习及总结(二):对象的解构

    一:数组的解构

    1:ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring)。本质上,这种写法属于“模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值

    let [foo, [[bar], baz]] = [1, [[2], 3]];
    foo // 1
    bar // 2
    baz // 3
    
    let [ , , third] = ["foo", "bar", "baz"];
    third // "baz"
    
    let [x, , y] = [1, 2, 3];
    x // 1
    y // 3
    
    let [head, ...tail] = [1, 2, 3, 4];
    head // 1
    tail // [2, 3, 4]
    
    let [x, y, ...z] = ['a'];
    x // "a"
    y // undefined
    z // []
    

    如果解构不成功,变量的值就等于undefined

    let [foo] = []; // foo为undefined
    let [bar, foo] = [1];  // foo为undefined
    

      

    另一种情况是不完全解构,即等号左边的模式,只匹配一部分的等号右边的数组。这种情况下,解构依然可以成功。

    2:默认值

    解构赋值允许指定默认值,只有当一个数组成员严格等于(===)undefined,默认值才会生效

    let [foo = true] = [];
    foo // true
    
    let [x, y = 'b'] = ['a']; // x='a', y='b'
    let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
    

    如果一个数组成员是null,默认值就不会生效,因为null不严格等于undefined

    let [x = 1] = [undefined];
    x // 1
    
    let [x = 1] = [null];
    x // null
    

    如果默认值是一个表达式,那么这个表达式是惰性求值的,即只有在用到的时候(值为undefined),才会求值。

    function f() {
      console.log('aaa');
    }
    
    let [x = f()] = [1]; // 这里的f() 不会执行
    
    function fff() {
      console.log('aaa');
    }
    
    let [x = fff()] = [undefined]; // 这里的fff() 会执行
    

    默认值可以引用解构赋值的其他变量,但该变量必须已经声明。

    let [x = 1, y = x] = [];     // x=1; y=1
    let [x = 1, y = x] = [2];    // x=2; y=2
    let [x = 1, y = x] = [1, 2]; // x=1; y=2
    let [x = y, y = 1] = [];     // ReferenceError: y is not defined
    

      

    二:对象的解构

    1:对象的解构与数组有一个重要的不同。数组的元素是按次序排列的,变量的取值由它的位置决定;而对象的属性没有次序,变量必须与属性同名,才能取到正确的值。

    let { foo, bar } = { foo: 'aaa', bar: 'bbb' };
    foo // "aaa"
    bar // "bbb"
    

    如果解构失败,变量的值等于undefined

    let {foo} = {bar: 'baz'};
    foo // undefined
    

    对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量。

    // 例一
    let { log, sin, cos } = Math;
    
    // 例二
    const { log } = console;
    log('hello') // hello
    

      
    2:默认值|
    对象的解构也可以指定默认值。默认值生效的条件是,对象的属性值严格等于undefined

    var {x = 3} = {};
    x // 3
    
    var {x, y = 5} = {x: 1};
    x // 1
    y // 5
    
    var {x: y = 3} = {};
    y // 3
    
    var {x: y = 3} = {x: 5};
    y // 5
    
    var { message: msg = 'Something went wrong' } = {};
    msg // "Something went wrong"
    
    var {x = 3} = {x: undefined};
    x // 3
    
    var {x = 3} = {x: null};
    x // null
    

      

    三:字符串的解构赋值

    字符串也可以解构赋值。这是因为此时,字符串被转换成了一个类似数组的对象。

    const [a, b, c, d, e] = 'hello';
    a // "h"
    b // "e"
    c // "l"
    d // "l"
    e // "o"
    

    类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值。

    let {length : len} = 'hello';
    len // 5
    

      

    四:数值和布尔值的解构赋值

    五:函数参数的解构赋值

    1:函数的参数也可以使用解构赋值。

    function add([x, y]){
      console.log(y, x) // 2, 1    
      return x + y;
    }
    
    add([1, 2]); // 3
    

    函数add的参数表面上是一个数组,但在传入参数的那一刻,数组参数就被解构成变量xy。对于函数内部的代码来说,它们能感受到的参数就是xy

    [[1, 2], [3, 4]].map(([a, b]) => a + b);
    // [ 3, 7 ]
    

    函数参数的解构也可以使用默认值。

    function move({x = 0, y = 0} = {}) {
      return [x, y];
    }
    
    move({x: 3, y: 8}); // [3, 8]
    move({x: 3}); // [3, 0]
    move({}); // [0, 0]
    move(); // [0, 0]
    

    下面的写法会得到不一样的结果。

    function move({x, y} = { x: 0, y: 0 }) {
      return [x, y];
    }
    
    move({x: 3, y: 8}); // [3, 8]
    move({x: 3}); // [3, undefined]
    move({}); // [undefined, undefined]
    move(); // [0, 0]

    上面代码是为函数move的参数指定默认值(这里是为{ x: 0, y: 0 }指定了默认值),而不是为变量xy指定默认值,所以会得到与前一种写法不同的结果。 

    undefined就会触发函数参数的默认值。

    [1, undefined, 3].map((x = 'yes') => x);
    // [ 1, 'yes', 3 ]
    

      

    六:用途

    1:交换变量的值

    let x = 1;
    let y = 2;
    
    [x, y] = [y, x];
    console.log(x,y); // 2,1
    

      

    2:从函数返回多个值

    返回一个数组

    function example() {
      return [1, 2, [3,4], {x: 1}];
    }
    let [a, b, c,d] = example();
    console.log(a,b,c,d) //  1,2,[3, 4],{x: 1}
    

    返回一个对象

    function example() {
      return {
        foo: 1,
        bar: 2
      };
    }
    let { foo, bar } = example();
    console.log(foo, bar) // 1,2
    

      

    3:函数参数的定义

     解构赋值可以方便地将一组参数与变量名对应起来。

    // 参数是一组有次序的值
    function f([x, y, z]) { ... }
    f([1, 2, 3]);
    
    // 参数是一组无次序的值
    function f({x, y, z}) { ... }
    f({z: 3, y: 2, x: 1});
    

      

    4:提取json的值

    let jsonData = {
      id: 42,
      status: "OK",
      data: [867, 5309]
    };
    
    let { id, status, data: number } = jsonData;
    
    console.log(id, status, number);
    // 42, "OK", [867, 5309]
    

      

    5:函数参数的默认值 (见第五点)

    jQuery.ajax = function (url, {
      async = true,
      beforeSend = function () {},
      cache = true,
      complete = function () {},
      crossDomain = false,
      global = true,
      // ... more config
    } = {}) {
      // ... do stuff
    };
    

      

  • 相关阅读:
    密码强度正则表达式 – 必须包含大写字母,小写字母和数字,至少8个字符等
    |DataDirectory|的使用
    SqlParameter的作用与用法
    C# 通过http post 请求上传图片和参数
    webuploader批量导入文件
    MVC使用Controller完成登录验证
    网页特效 模板素材
    C#中的Cookie 添加 读取 删除
    JS jquery jquery.wordexport.js 实现导出word
    初始配置JDK
  • 原文地址:https://www.cnblogs.com/momozjm/p/12337569.html
Copyright © 2011-2022 走看看