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. HPUX 11.31 MC-SG SGeRAC配置

    HPUX 11.31 MC-SG SGeRAC配置 环境: 系统版本号 hp-unix 11.3v2 1503 serviceguard extension版本号 T1907 实施 1. 磁盘空间划分 ...

  2. Linux禁用显示“缓冲调整”

    Linux禁用显示"缓冲调整" youhaidong@youhaidong-ThinkPad-Edge-E545:~$ free -o total used free shared ...

  3. canvas图形函数

    function drawStar(cobj,x, y, radius1, radius2, num, drawType, color) {//参数:画笔,圆心X.圆心Y,半径1,半径2,形状边,实心 ...

  4. 【Hdu2089】不要62(数位DP)

    Description 题目大意:给定区间[n,m],求在n到m中没有"62"或"4"的数的个数. 如62315包含62,88914包含4,这两个数都是不合法的 ...

  5. Linux 学习记录 一(安装、基本文件操作).

         Linux distributions主要分为两大系统,一种是RPM方式安装软件的系统,包括Red Hat,Fedora,SuSE等都是这类:一种则是使用Debian的dpkg方式安装软件的 ...

  6. Fragment生命周期及实现点击导航图片切换fragment,Demo

    PS:Fragment简介 Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会 ...

  7. SSIS 实用表达式部分总结

    下面,列出一些实用的表达式: 1,路径取文件名 RIGHT([FilePath],FINDSTRING(REVERSE([FilePath]),) - ) RIGHT(@[User::FilePath ...

  8. 伽罗瓦域(有限域)GFq^12上元素的1→2→4→12塔式扩张(2)------第二次扩张

    接上文https://www.cnblogs.com/heshuchao/p/8196307.html 继续探讨塔式扩张的第二部分,即1→2→4→12中2 → 4的元素扩张表示方式与计算公式推导. 3 ...

  9. 鸟哥的linux私房菜学习-(三)X Window与文本模式的切换

    通常我们也称文本模式为终端机接口, terminal 或 console喔!Linux默认的情况下会提供六个Terminal来让使用者登陆, 切换的方式为使用:[Ctrl] + [Alt] + [F1 ...

  10. NPOI 1.2.5 教程

    NPOI1.2.5教程官方地址 作者:Tony Qu & atao.xiang QQ群:20144214 ===== 持续更新中 ===== a. NPOI简介 b. 版权声明 目录 1. O ...