zoukankan      html  css  js  c++  java
  • es6--解构

    let node = {
            type : 'identifier',
            name : 'foo'
    };
    let {type,name} = node;
    console.log(type);//'identifier'
    console.log(name);//'foo
    let node = {
            type : 'identifier',
            name : 'foo'
    };
    let {type:localType,name:localName} = node;
    console.log(localType);//'identifier'
    console.log(localName);//'foo'

    注意点

    • 使用var、let、const对对象进行解构时,必须提供初始化器(即等号右边的值)
    • 不使用var、let、const赋值时,需要将解构语句使用()进行包裹
    ({type,name} = node);//{}在js中作为代码块,单独使用加等号会报错会报错
    默认值
    当你使用解构赋值语句时,如果指定的本地变量没有同名属性,那么该变量会被赋值为undefined,可以对其进行指定默认值
     let node = {
          type : 'identifier',
          name : 'foo'
      };
      let {type,name,val} = node;
      console.log(val);//undefined
      ({type,name,val = '234'}  = node)
      console.log(val);//'234'
        嵌套的对象解构
        使用类似于对象字面量的语法,可以深入到嵌套的对象结构中去提取你想要的数据
    
     let node = {
         type: "Identifier",
         name: "foo",
         loc: {
           start: {
                 line: 1,
                 column: 1
            },
           end: {
                 line: 1,
                 column: 4
            }
        }
    };
    let { loc: { start }} = node;
    console.log(start.line); // 1
    console.log(start.column); // 1
    这种方法使得本地变量start被赋值node中的loc的start对象,值得注意的是这种操作与直接node.loc.start的赋值是一致的,所以要注意值类型与引用类型的区别
    
    注意点:此语句中并没有任何变量被绑定
    
       // 没有变量被声明!
      let { loc: {} } = node;
  • 相关阅读:
    [718. 最长重复子数组]
    排序算法--归并,堆,快速排序
    改进的插排--希尔排序
    排序算法--选泡插
    对封装继承多态的理解
    Servlet[springmvc]的Servlet.init()引发异常
    [面试题 16.18. 模式匹配]
    [124. 二叉树中的最大路径和](
    7.29_python_lx_day11
    7.28_python_lx_day18
  • 原文地址:https://www.cnblogs.com/jentary/p/13223456.html
Copyright © 2011-2022 走看看