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;
  • 相关阅读:
    http从发出请求到接收响应的旅行
    git(二)github的使用入门及搜索技巧
    git(一) 基础
    获取基于Internet Explorer_Server的聊天窗口内容
    主机字节与网络字节的转换
    SQL Server存储过程中防止线程重入处理方式
    利用NVelocity 模版生成文本文件
    C# async await 学习笔记2
    C# async await 学习笔记1
    imx6 工具链下载地址
  • 原文地址:https://www.cnblogs.com/jentary/p/13223456.html
Copyright © 2011-2022 走看看