一、变量的解构赋值
1.数组的解构赋值:
以前为变量赋值:
let a = 1;
let b = 2;
let c = 3;
ES6允许写成下面这样:
let [a, b, c] = [1, 2, 3];
这种写法表示,从数组中提取值,按照对应位置,对变量赋值
再来看更多例子:
let [foo, [[bar], baz]] = [1, [[2], 3]];
foo // 1
bar // 2
baz // 3
let [ , , third] = ["foo", "bar", "baz"];
third // "baz"
let [x, , y] = [1, 2, 3];
x // 1
y // 3
let [head, ...tail] = [1, 2, 3, 4];
head // 1
tail // [2, 3, 4]
let [x, y, ...z] = ['a'];
x // "a"
y // undefined
z // []
//解构不成功的情况:
let [foo] = [];
let [bar, foo] = [1];
foo//undefined
//不完全解构:即等号左边的模式,只匹配一部分的等号右边的数组
let [x, y] = [1, 2, 3];
x // 1
y // 2
let [a, [b], d] = [1, [2, 3], 4];
a // 1
b // 2
d // 4
另外,如果等号的右边不是数组,(或者严格讲,不是可遍历的结构),那么就会报错:
// 报错
let [foo] = 1;
let [foo] = false;
let [foo] = NaN;
let [foo] = undefined;
let [foo] = null;
let [foo] = {};
事实上,只要某种数据结构具有 Iterator 接口,都可以采用数组形式的解构赋值:
function* fibs() {
let a = 0;
let b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
let [first, second, third, fourth, fifth, sixth] = fibs();
sixth // 5
默认值:
解构赋值允许指定默认值
let [foo = true] = [];
foo // true
let [x, y = 'b'] = ['a']; // x='a', y='b'
let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
//但要注意的是,null不严格等于undefined,所以默认值不会生效
let [x = 1] = [undefined];
x // 1
let [x = 1] = [null];
x // null
此外,关于默认值是表达式的情况:
function f() {
console.log('aaa');
}
let [x = f()] = [1];
上述代码,因为x能取到值1,所以f函数不会根本执行,总结一下,当默认值是表达式的时候,那么这个表达式是惰性求值的,即只有在用的时候才会求值
2.对象的解构赋值:
除了数组,解构同时也可用于对象
let { foo, bar } = { foo: 'aaa', bar: 'bbb' };
foo // "aaa"
bar // "bbb"
let { bar, foo } = { foo: 'aaa', bar: 'bbb' };
foo // "aaa"
bar // "bbb"
//解构失败,变量的值就是undefined
let { baz } = { foo: 'aaa', bar: 'bbb' };
baz // undefined
对象解构和数组解构的不同:数组元素是按次序排列的,变量取值由它的位置决定;而对象的属性没有次序,变量必须和属性同名才能取到正确的值
利用解构赋值,可以很方便的将先有对象的方法,赋值到某个变量(深有体会)
// 例一
let { log, sin, cos } = Math;
// 例二
const { log } = console;
log('hello') // hello
那么,如果变量名与属性名不一致,必须这样写:
let { foo: baz } = { foo: 'aaa', bar: 'bbb' };
baz // "aaa"
let obj = { first: 'hello', last: 'world' };
let { first: f, last: l } = obj;
f // 'hello'
l // 'world'
所以思考一下,实际上,对象的解构赋值是下面形式的简写:
let { foo: foo, bar: bar } = { foo: 'aaa', bar: 'bbb' };
也就是说,对象的解构赋值的内部机制,是先找到同名属性,然后再赋给对应的变量。真正被赋值的是后者,而不是前者
默认值:
参考数组解构赋值的默认值,右边同样必须严格等于undefined,默认值才会生效
3.字符串的解构赋值:
字符串也可以解构赋值。这是因为此时,字符串被转换成了一个类似数组的对象。
const [a, b, c, d, e] = 'hello';
a // "h"
b // "e"
c // "l"
d // "l"
e // "o"
类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值。
let {length : len} = 'hello';
len // 5
4.数值和布尔值的解构赋值:
解构赋值时,如果等号右边是数值和布尔值,则会先转为对象。
//数值和布尔值的包装对象都有toString属性,因此变量s都能取到值
let {toString: s} = 123;
s === Number.prototype.toString // true
let {toString: s} = true;
s === Boolean.prototype.toString // true
解构赋值的规则是,只要等号右边的值不是对象或数组,就先将其转为对象。由于undefined和null无法转为对象,所以对它们进行解构赋值,都会报错。
let { prop: x } = undefined; // TypeError
let { prop: y } = null; // TypeError
5.函数参数的解构赋值:
function add([x, y]){
return x + y;
}
add([1, 2]); // 3
[[1, 2], [3, 4]].map(([a, b]) => a + b);
// [ 3, 7 ]
使用默认值:
function move({x = 0, y = 0} = {}) {
return [x, y];
}
move({x: 3, y: 8}); // [3, 8]
move({x: 3}); // [3, 0]
move({}); // [0, 0]
move(); // [0, 0]
注意,下面的代码是为函数move的参数指定默认值,而不是给变量x,y指定默认值:
function move({x, y} = { x: 0, y: 0 }) {
return [x, y];
}
move({x: 3, y: 8}); // [3, 8]
move({x: 3}); // [3, undefined]
move({}); // [undefined, undefined]
move(); // [0, 0]