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;
  • 相关阅读:
    Java内存模型
    BigDecimal踩过的大坑
    Java开发小技巧
    多线程同步辅助工具类
    ReentrantLock中的公平锁与非公平锁
    ReentrantLock与synchronized的区别
    推荐一个Java设计模式写的很好的博客
    线程池ThreadPoolExecutor工作原理
    支付系统架构设计转载
    linux 部署脚本
  • 原文地址:https://www.cnblogs.com/jentary/p/13223456.html
Copyright © 2011-2022 走看看