ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称作解构。
数组的解构赋值
let [a, b, c] = [1, 2, 3];
从数组中提取值,按照对应位置,对变量赋值。
本质上,这种写法属于“模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值。
不完全解构,即等号左边的模式,只匹配一部分的等号右边的数组。这种情况下,解构依然可以成功。
let [a, [b], d] = [1, [2, 3], 4]; // a=1,b=2,d=4
如果解构不成功,变量的值就等于undefined。foo的值是undefined。
let [foo] = [];
let [bar, foo] = [1];
对于 Set 结构,也可以使用数组的解构赋值。
let [x, y, z] = new Set(['a', 'b', 'c']); // x='a',y='b',z='d'
如果等号的右边不是数组(或者严格地说,不是可遍历的结构),那么将会报错。
let [foo] = 1;
let [foo] = false;
let [foo] = NaN;
let [foo] = undefined;
let [foo] = null;
let [foo] = {};
默认值,ES6 内部使用严格相等运算符(===),判断一个位置是否有值。所以,只有当一个数组成员严格等于undefined,默认值才会生效。
let [x = 1] = [undefined]; // x = 1
let [x = 1] = [null]; // x = null
如果默认值是一个表达式,那么这个表达式是惰性求值的,即只有在用到的时候,才会求值。
默认值可以引用解构赋值的其他变量,但该变量必须已经声明。
let [x = 1, y = x] = []; // x=1; y=1
let [x = 1, y = x] = [2]; // x=2; y=2
let [x = 1, y = x] = [1, 2]; // x=1; y=2
let [x = y, y = 1] = []; // ReferenceError: y is not defined
应用场景
变量交换
let a=1;
let b=2;
[a,b]=[b,a]
// a=2,b=1
取函数的返回值
function fn(){
return [1,2,3];
}
let a,b;
[a,,b]=fn();
// a=1,b=3
由于数组本质是特殊的对象,因此可以对数组进行对象属性的解构。
对象的解构赋值
对象的解构与数组有一个重要的不同。数组的元素是按次序排列的,变量的取值由它的位置决定;而对象的属性没有次序,变量必须与属性同名,才能取到正确的值。
let { bar, foo } = { foo: 'aaa', bar: 'bbb' };
foo // "aaa"
bar // "bbb"
如果变量名与属性名不一致,必须写成下面这样。
let { foo: baz } = { foo: 'aaa', bar: 'bbb' };
baz // "aaa"
// 对象的解构赋值,是下面形式的简写
let { foo: foo, bar: bar } = { foo: 'aaa', bar: 'bbb' };
//foo是匹配的模式,baz才是变量。真正被赋值的是变量baz,而不是模式foo。
let { foo: baz } = { foo: 'aaa', bar: 'bbb' };
baz // "aaa"
foo // error: foo is not defined
对象的解构赋值的内部机制,是先找到同名属性,然后再赋给对应的变量。真正被赋值的是后者,而不是前者。
解构失败,变量的值等于undefined
let {foo}={bar:'baz'};
foo // undefined
默认值,生效的条件是,对象的属性值严格等于undefined。
对象的解构赋值可以取到继承的属性。
const obj1={};
const obj2={foo:'bar'};
Object.setPrototypeOf(obj1,obj2);
const { foo }=obj1;
foo // 'bar'
Object.setPrototypeOf() 设置一个指定的对象的原型到另一个对象或null。
Object.setPrototypeOf(obj,prototype)
obj:要设置其原型的对象;prototype:该对象的新原型。
相对于Object.prototype.proto,它被认为是修改对象原型更合适的方法。
嵌套结构
let obj = {
p: [
'Hello',
{ y: 'World' }
]
};
let { p: [x, { y }] } = obj; // p是模式
x // "Hello"
y // "World"
let obj = {
p: [
'Hello',
{ y: 'World' }
]
};
let { p, p: [x, { y }] } = obj; // p是变量
x // "Hello"
y // "World"
p // ["Hello", {y: "World"}]
应用
对象的解构赋值,可以很方便地将现有对象的方法,赋值到某个变量。
// 例一
let { log, sin, cos } = Math;
// 例二
const { log } = console;
log('hello') // hello
let metaData={
title:'abc',
test:[{
title:'test',
desc:'description'
}]
}
let {title:esTitle,test:[{title:cnTitle}]}=metaData;
// esTitle abc,cnTitle test
提取JSON数据
let jsonData = {
id: 42,
status: "OK",
data: [867, 5309]
};
let { id, status, data: number } = jsonData;
console.log(id, status, number);
// 42, "OK", [867, 5309]
字符串的解构赋值
字符串也可以解构赋值。这是因为此时,字符串被转换成了一个类似数组的对象。
类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值。
let {length:len}='hello';
len // 5
数值和布尔值的解构赋值
解构赋值的规则是,只要等号右边的值不是对象或数组,就先将其转为对象。由于undefined和null无法转为对象,所以对它们进行解构赋值,都会报错。
let {toString: s} = 123;
s === Number.prototype.toString // true
let {toString: s} = true;
s === Boolean.prototype.toString // true
let { prop: x } = undefined; // TypeError
let { prop: y } = null; // TypeError
函数参数的解构赋值
[[1,2],[3,4]].map(([a,b])=>a+b);
// [3,7]
不能使用圆括号的情况
(1)变量声明语句
// 全部报错
let [(a)] = [1];
let {x: (c)} = {};
let ({x: c}) = {};
let {(x: c)} = {};
let {(x): c} = {};
(2)函数参数
函数参数也属于变量声明,因此不能带有圆括号。
(3)赋值语句的模式
// 全部报错
({ p: a }) = { p: 42 };
[({ p: a }), { x: c }] = [{}, {}];
([a]) = [5];
可以使用圆括号的情况
赋值语句的非模式部分,可以使用圆括号
[(b)] = [3]; // 正确
[(parseInt.prop)] = [3]; // 正确
({ p: (d) } = {}); // 正确
应用
遍历Map结构
任何部署了Iterator接口的对象,都可以用for…of循环遍历,Map结构原生支持Iterator接口,配合变量的解构赋值,获取键名和键值就非常方便。
const map=new Map();
map.set('first','hello');
map.set('second','world');
for(let [key,value] of map){}
for(let [key] of map){}
for(let [,value] of map){}