【读书笔记】【深入理解ES6】#5-解构:使数据访问更便捷
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-解构:使数据访问更便捷的更多相关文章
- 深入理解ES6之解构
变量赋值的痛 对象 let o = {a:23,b:34}; let a = o.a; let b = o.b; 如上文代码,我们经常会遇到在各种场合需要获取对象中的值的场景,舒服一点的是获取单个属性 ...
- 进军es6(2)---解构赋值
本该两周之前就该总结的,但最近一直在忙校招实习的事,耽误了很久.目前依然在等待阿里HR面后的结果中...但愿好事多磨!在阿里的某轮面试中面试官问到了es6的掌握情况,说明es6真的是大势所趋,我们更需 ...
- ES6 的解构赋值前每次都创建一个对象吗?会加重 GC 的负担吗?
本文来源于知乎上的一个提问. 为了程序的易读性,我们会使用 ES6 的解构赋值: function f({a,b}){} f({a:1,b:2}); 这个例子的函数调用中,会真的产生一个对象吗?如果会 ...
- ES6 对象解构
ES6 对象解构 第一眼看到,什么鬼? const { body } = document `` 其实等于: const body = document.body ``` http://es6.rua ...
- es6的解构赋值学习(1)
相对es5的简单的"="赋值来说,es6增加了一种新的赋值模式--解构赋值,按照它的规则,可以从数组和对象中提取值来对变量进行赋值,个人觉得方便了很多,但是这个模式有点恶心人,相比 ...
- Es6 新增解构赋值
1.数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 要想实现解构,就必须是容器,或者具有可遍历的接口. 以前,为 ...
- ES6 之 解构赋值
本博文配合 阮一峰 <ES6 标准入门(第3版)>一书进行简要概述 ES6 中变量的解构赋值. 数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这 ...
- ES6之解构赋值
截止到ES6,共有6种声明变量的方法,分别是var .function以及新增的let.const.import和class: 我们通常的赋值方法是: var foo='foo'; function ...
- ES6多层解构
const info = { person: { name: 'xiaobe', other: { age: 22, } }, song: 'rolling', } // 解构person的内容 co ...
随机推荐
- 自己定义定时器(Timer)
近期做项目的时候,用到了java.util.Timer定时器类.也初步使用了,个人感觉不错.只是,在某些方面Timer类无法满足项目的需求.比方,在使用Timer时,调用schedule()方法之后( ...
- Android的init过程:init.rc解析流程
这几天打算看下安卓的代码,看优秀的源代码也是一种学习过程,看源代码的过程就感觉到,安卓确实是深受linux内核的影响,不少数据结构的使用方法全然一致.花了一中午时间,研究了下init.rc解析过程,做 ...
- cs231n --- 3 : Convolutional Neural Networks (CNNs / ConvNets)
CNN介绍 与之前的神经网络不同之处在于,CNN明确指定了输入就是图像,这允许我们将某些特征编码到CNN的结构中去,不仅易于实现,还能极大减少网络的参数. 一. 结构概述 与一般的神经网络不同,卷积神 ...
- 微信小程序路过
应该算是入门篇, 从我怎么0基础然后沿着什么方向走,遇到的什么坑,如何方向解决,不过本人接触不是很多,所以也就了解有限. 小程序的前提: 1.小程序大小不允许超过2M.(也就是本地图片,大图精图不要在 ...
- dubbo,eclipse,服务报错
运行e3-manager报错找不到类,更新了子模块,但e3-manager下没有更新:
- Django实战,小网站实现增删改查
直接上代码 视图: from django.shortcuts import render,render_to_response, redirect from submit import models ...
- LeetCode中的最大子串和问题(Maximum Subarray)
问题描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...
- ASP.NET MVC下自定义错误页和展示错误页的几种方式
在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...
- bzoj 2427: [HAOI2010]软件安装
Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...
- uptime 命令详解
作用: 打印系统总共运行了多长时间和系统的平均负载. uptime 命令可以显示的信息依次为: 现在时间, 系统已经运行时间, 目前登录用户个数, 系统1,5,15 分钟内的平均负载 实例: up ...