ES6容许按照必定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构。解构赋值能够用var、let、const声明。且凡是具备Iterator接口的对象均可以进行解构赋值。而且解构的同时能够为其指定默认值。javascript
1.数组的解构赋值java
// ES5 var a = 1, b = 2, c = 3; // ES6 let [a, b, c] = [1, 2, 3]; console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 let [a, [b, c]] = [1, [2, 3]]; console.log(a); //1 console.log(b); //2 console.log(c); //3
本质上,这种写法属于“模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值。es6
let [a, b] = ['a']; console.log(b) // undefined
若是解构失败的话,对应的变量的值就是undefined。json
let [a, b, c] = [1, 2, 3, d]; console.log(a); // 1 console.log(b); // 2 console.log(c); // 3
若是左面的变量只匹配到了右面的部分,则解构不彻底。数组
let [a, b = 2] = [1]; console.log(a); // 1 console.log(b); // 2 let [a = 1, b = a] = []; console.log(a); // 1 console.log(b); // 1 let [a = b, b = 2] = []; console.log(a); // b is not defined
咱们能够在解构的同时为变量设置默认值。而且变量能够引用其余的解构变量,但这时,变量必须提早声明。函数
2.对象的解构赋值ui
var { foo, bar } = { foo: 1, bar: 2 }; foo // 1 bar // 2
数组的元素是按次序排列的,变量的取值由它的位置决定;而对象的属性没有次序,变量必须与属性同名,才能取到正确的值。code
let { bar, foo } = { foo: 1, bar: 2 }; foo // 1 bar // 2 let { foo: a, bar: b} = { foo: 1, bar: 2}; a // 1 b // 2 let { foo: baz } = { foo: "aaa", bar: "bbb" }; baz // "aaa" foo // error: foo is not defined
对象的解构赋值的内部机制,是先找到同名属性,而后再赋给对应的变量。真正被赋值的是后者。对象
let foo; let {foo} = {foo: 1}; // SyntaxError: Duplicate declaration "foo" let baz; let {bar: baz} = {bar: 1}; // SyntaxError: Duplicate declaration "baz"
注意,采用这种写法时,变量的声明和赋值是一体的。对于let
和const
来讲,变量不能从新声明,因此一旦赋值的变量之前声明过,就会报错。上面代码中,解构赋值的变量都会从新声明,因此报错了。不过,由于var
命令容许从新声明,因此这个错误只会在使用let
和const
命令时出现。若是没有第二个let
命令,上面的代码就不会报错。接口
let {x = 3} = {}; x // 3 let {x, y = 5} = {x: 1}; x // 1 y // 5 let {x:y = 3} = {}; y // 3 let {x:y = 3} = {x: 5}; y // 5
默认值生效的条件是,对象的属性值严格等于undefined
。
let {x = 3} = {x: undefined}; x // 3 let {x = 3} = {x: null}; x // null
上面代码中,若是x
属性等于null
,就不严格相等于undefined
,致使默认值不会生效。若是解构失败,变量的值等于undefined
。
// 报错 var {foo: {bar}} = {baz: 'baz'};
上面代码中,等号左边对象的foo
属性,对应一个子对象。该子对象的bar
属性,解构时会报错。foo
这时等于undefined
,再取子属性就会报错。
3.字符串的解构
let [a, b, c, d, e] = 'hello'; a // "h" b // "e" c // "l" d // "l" e // "o" let {length : length} = 'hello'; length // 5
此时,字符串被转换成了一个相似数组的对象。相似数组的对象都有一个length
属性,所以还能够对这个属性解构赋值。
4.函数参数的解构赋值
function add([x, y]){ return x + y; } add([1, 2]);
5.使用场景
变量的解构赋值用途不少。
a . 交换变量的值
[x, y] = [y, x];
b . 函数返回多个值
// 返回一个数组 function example() { return [1, 2, 3]; } let [a, b, c] = example(); // 返回一个对象 function example() { return { foo: 1, bar: 2 }; } let { foo, bar } = example();
c . 函数参数
// 参数有序 function f([x, y, z]) { ... } f([1, 2, 3]); // 参数无序 function f({x, y, z}) { ... } f({z: 3, y: 2, x: 1});
d . 提取JSON
let json = { a: 1, b: 2, c: 3 }; let { a, b, c } = json;
上面代码能够快速提取JSON数据的值。
e . 遍历Map结构
var map = new Map(); map.set('first', 'hello'); map.set('second', 'world'); for (let [key, value] of map) { console.log(key + " is " + value); } // first is hello // second is world
若是只想获取键名,或者只想获取键值。
// 获取键名 for (let [key] of map) { // ... } // 获取键值 for (let [,value] of map) { // ... }
f . 输入模块的指定方法
const { SourceMapConsumer, SourceNode } = require("source-map");
参考自 : http://es6.ruanyifeng.com/#docs/destructuring