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"
},
type = "Literal",
name = 5; // 使用解构语法为多个变量赋值
// 注意:这里要在表达式的外层套一对小括号,否则{}会被视为一个代码块
({type, name} = node); console.log(type); // "Identifier"
console.log(name); // "foo"

解构表达式的值与表达式右侧(也就是=右侧)的值相等。

let node = {
type: "Identifier",
name: "foo"
},
type = "Literal",
name = 5; function outputInfo(value) {
console.log(value === node); // true
} // 调用方法的同时使用解构表达式赋值
// 表达式的值为等号(=)右侧的值
outputInfo({type, name} = node); console.log(type); // "Identifier"
console.log(name); // "foo"

默认值

使用解构表达式时,如果指定的局部变量名称在对象中不存在,name这个局部变量会被赋值为 undefined。

let node = {
type: "Identifier",
name: "foo"
}; let { type, name, value } = node; console.log(type); // "Identifier"
console.log(name); // "foo"
console.log(value); // undefined

也可以为不存在的局部变量定义默认值。

只有当解析对象中没有该属性或该属性值为undefined时,默认值才会生效。

let node = {
type: "Identifier",
name: "foo"
}; let { type, name, value = true } = node; console.log(type); // "Identifier"
console.log(name); // "foo"
console.log(value); // true

为非同名变量赋值

let node = {
type: "Identifier",
name: "foo"
}; // 使用解构语法为多个变量赋值
let {type: localType, name: localName} = node; console.log(localType); // "Identifier"
console.log(localName); // "foo"

写法同对象字面量的语法相反,值在左边,变量在右边。

也可以在为非同名变量赋值的同时为其赋默认值。

let node = {
type: "Identifier"
}; // 使用解构语法为多个变量赋值
let {type: localType, name: localName = "bar"} = node; console.log(localType); // "Identifier"
console.log(localName); // "bar"

嵌套对象解构

解构嵌套对象仍然与对象字面量的语法相似。

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

上例在解构模式中使用了花括号,其含义为在找到node对象中的loc属性后,应当深入一层继续查找start属性。

所有冒号前的标识符都代表在对象中的检索位置,其右侧为被赋值的变量名;

如果冒号后是花括号,则意味着要赋予的最终值嵌套在对象内部更深的层级中。

也可以使用一个与对象属性名不同的局部变量名。

let node = {
type: "Identifier",
name: "foo",
loc: {
start: {
line: 1,
column: 1
},
end: {
line: 1,
column: 4
}
}
}; let { loc: { start: localStart } } = node; console.log(localStart.line); // 1
console.log(localStart.column); // 1

数组解构

数组解构使用的是数组字面量,且解构操作全部在数组内完成。

let colors = [ "red", "green", "blue" ];
let [ firstColor, secondColor ] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"

省略部分元素,只为感兴趣的元素提供变量名

let colors = [ "red", "green", "blue" ];
let [ , , thirdColor ] = colors;
console.log(thirdColor); // "blue"

解构赋值

let colors = [ "red", "green", "blue" ],
firstColor = "black",
secondColor = "purple";
[ firstColor, secondColor ] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"

使用数组解构交换两个变量的值

let a = 1,
b = 2; [a, b] = [b, a]; console.log(a); // 2
console.log(b); // 1

默认值

当指定位置的属性不存在或其值为 undefined 时才会使用默认值。

let colors = [ "red" ];
let [ firstColor, secondColor = "green" ] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"

嵌套数组解构

与嵌套对象解构的语法类似。

let colors = [ "red", [ "green", "lightgreen" ], "blue" ];
let [ firstColor, [ secondColor ] ] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"

不定元素

通过 ... 语法将数组中的其余元素赋值给一个特定的变量。

let colors = [ "red", "green", "blue" ];
let [ firstColor, ...restColors ] = colors;
console.log(firstColor); // "red"
console.log(restColors.length); // 2
console.log(restColors[0]); // "green"
console.log(restColors[1]); // "blue"

可以通过不定元素实现数组赋值的功能(也可以通过concat方法实现)

let colors = [ "red", "green", "blue" ];
let [ ...clonedColors ] = colors;
console.log(clonedColors); // ["red", "green", "blue"]

Note

在被解构的数组中,不定元素必须为最后一个条目,在后面继续添加逗号会导致程序抛出语法错误。

混合解构

let node = {
type: "Identifier",
name: "foo",
loc: {
start: {
line: 1,
column: 1
},
end: {
line: 1,
column: 4
}
},
range: [0, 3]
}; let {
loc: { start },
range: [ startIndex ]
} = node; console.log(start.line); // 1
console.log(start.column); // 1
console.log(startIndex); // 0

解构参数

function setCookie(name, value, { secure, path, domain, expires }) {

}

setCookie("type", "js", {
secure: true,
expires: 60000
});

第三个参数传入的是对象,方法参数使用参数解构自动提供属性值到对应的参数。

Note

解构参数支持上面所有的解构特性。

必须传值的解构参数

第三个参数不传或者为null、undefined时,解析会出错。

需要为其提供默认值来解决这个问题。

function setCookie(name, value, { secure, path, domain, expires } = { }) {

}

解构参数的默认值

为解构参数中的某个或全部参数指定默认值

const setCookieDefaults = {
secure: false,
path: "/",
domain: "liujiajia.me",
expires: new Date(Date.now() + 360000000)
} function setCookie(name, value, {
secure = setCookieDefaults.secure,
path = setCookieDefaults.path,
domain = setCookieDefaults.domain,
expires = setCookieDefaults.expires
} = setCookieDefaults) {
// ...
}

【读书笔记】【深入理解ES6】#5-解构:使数据访问更便捷的更多相关文章

  1. 深入理解ES6之解构

    变量赋值的痛 对象 let o = {a:23,b:34}; let a = o.a; let b = o.b; 如上文代码,我们经常会遇到在各种场合需要获取对象中的值的场景,舒服一点的是获取单个属性 ...

  2. 进军es6(2)---解构赋值

    本该两周之前就该总结的,但最近一直在忙校招实习的事,耽误了很久.目前依然在等待阿里HR面后的结果中...但愿好事多磨!在阿里的某轮面试中面试官问到了es6的掌握情况,说明es6真的是大势所趋,我们更需 ...

  3. ES6 的解构赋值前每次都创建一个对象吗?会加重 GC 的负担吗?

    本文来源于知乎上的一个提问. 为了程序的易读性,我们会使用 ES6 的解构赋值: function f({a,b}){} f({a:1,b:2}); 这个例子的函数调用中,会真的产生一个对象吗?如果会 ...

  4. ES6 对象解构

    ES6 对象解构 第一眼看到,什么鬼? const { body } = document `` 其实等于: const body = document.body ``` http://es6.rua ...

  5. es6的解构赋值学习(1)

    相对es5的简单的"="赋值来说,es6增加了一种新的赋值模式--解构赋值,按照它的规则,可以从数组和对象中提取值来对变量进行赋值,个人觉得方便了很多,但是这个模式有点恶心人,相比 ...

  6. Es6 新增解构赋值

    1.数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 要想实现解构,就必须是容器,或者具有可遍历的接口. 以前,为 ...

  7. ES6 之 解构赋值

    本博文配合 阮一峰 <ES6 标准入门(第3版)>一书进行简要概述 ES6 中变量的解构赋值. 数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这 ...

  8. ES6之解构赋值

    截止到ES6,共有6种声明变量的方法,分别是var .function以及新增的let.const.import和class: 我们通常的赋值方法是: var foo='foo'; function ...

  9. ES6多层解构

    const info = { person: { name: 'xiaobe', other: { age: 22, } }, song: 'rolling', } // 解构person的内容 co ...

随机推荐

  1. Vue深度学习(2)

    Text 可以在表单的input 元素上使用v-model 指令来创建双向数据绑定.它会根据input元素的类型自动选取正确的绑定模式. <div id="app"> ...

  2. vue-router实例

    最近刚刚用vue写了个公司项目,使用vue-cli构建的,算是中大型项目吧,然后这里想记录并且分享一下其中的知识点,希望对大家有帮助,后期会逐渐分享:话不多说,直接上代码!! main.js // T ...

  3. Natas Wargame Level27 Writeup(SQL表的注入/溢出与截取)

    前端: <html> <head> <!-- This stuff in the header has nothing to do with the level --&g ...

  4. springboot 入门七-静态资源处理

    Spring Boot 默认配置的/**映射到/static(或/public ,/META-INF/resources),/webjars/**会映射到classpath:/META-INF/res ...

  5. jemeter——badboy导入的jmx文件自带元件解析

    线程组设置与解析 含义:1秒启动100个线程,每个线程循环调用20次请求  (包括FTP请求.Java请求.http请求,根据你提交的请求而定) delay thread creation until ...

  6. Oracle11g不能导出空表问题

    ORACLE 11g 用exp命令导出库文件备份时,发现只能导出来一部分表而且不提示错误,之前找不到解决方案只能把没导出来的表重新建建立.后来发现是所有的空表都没有导出来.于是想好好查查,因为在以前的 ...

  7. 张高兴的 Xamarin.Forms 开发笔记:Android 快捷方式 Shortcut 应用

    一.Shortcut 简介 Shortcut 是 Android 7.1 (API Level 25) 的新特性,类似于苹果的 3D Touch ,但并不是压力感应,只是一种长按菜单.Shortcut ...

  8. 开源API测试工具 Hitchhiker v0.7更新 - Schedule的对比diff

    Hitchhiker 是一款开源的支持多人协作的 Restful Api 测试工具,支持Schedule, 数据对比,压力测试,支持脚本定制请求,可以轻松部署到本地,和你的team成员一起协作测试Ap ...

  9. PredictionIO+Universal Recommender快速开发部署推荐引擎的问题总结(2)

    1, 对Universal Recommender进行pio build成功,但是却提示No engine found Building and delpoying model [INFO] [Eng ...

  10. lesson - 6 Linux下磁盘管理

    1. 查看磁盘或者目录的容量df  查看磁盘各分区使用情况   不加参数以k为单位   df -i inode数,df -h  以G或者T或者M   df -m  以M单位显示  du 查看目录或者文 ...