ES6 容许按照必定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构
<!--more-->git
let [a, b, c] = [1, 2, 3]
能够按照对应的位置,对变量进行赋值
此种写法至关于模式匹配,例子以下:github
// 数组嵌套赋值 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 // []
解构赋值失败,则变量的值为undefined
json
let [foo] = [] let [bar, foo] = [1]
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] = {};
let [foo = true] = []; foo // true let [x, y = 'b'] = ['a']; // x='a', y='b' let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
默认对象生效的条件是该对象对应的值必须严格等于undefined
,不然不会生效函数
let [x = 1] = [undefined]; x // 1 let [x = 1] = [null]; x // null
let { foo, bar } = { foo: "aaa", bar: "bbb" }; foo // "aaa" bar // "bbb"
变量的名称必须与对应的属性值同名才能够取到正确的值ui
实质上以下:先找到对象的同名属性,而后赋值给对应的变量prototype
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'
note:code
undefined
若是解构模式是嵌套的对象,并且子对象所在的父属性不存在,那么将会报错。对象
// 报错 let {foo: {bar}} = {baz: 'baz'};
此时foo的值已经为undefined
,而后又取它的子属性的值天然会报错ip
避免大括号在首位,以下报错是由于JavaScript引擎将其认为是代码块
// 错误的写法 let x; {x} = {x: 1}; // SyntaxError: syntax error // 正确的写法 let x; ({x} = {x: 1});
与数组相似
首先会将等号右边的数值或者布尔值转换为对象,而后进行解构赋值
let {toString: s} = 123; s === Number.prototype.toString // true let {toString: s} = true; s === Boolean.prototype.toString // true
但undefined
和null
不能转换为对象,因此对他们解构赋值会报错
function add([x, y]){ return x + y; } add([1, 2]); // 3
如下代码是为函数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]
// 所有报错 let [(a)] = [1]; let {x: (c)} = {}; let ({x: c}) = {}; let {(x: c)} = {}; let {(x): c} = {}; let { o: ({ p: p }) } = { o: { p: 2 } };
// 报错 function f([(z)]) { return z; } // 报错 function f([z,(x)]) { return x; }
// 所有报错 ({ p: a }) = { p: 42 }; ([a]) = [5];
赋值语句的非模式部分
[(b)] = [3]; // 正确 ({ p: (d) } = {}); // 正确 [(parseInt.prop)] = [3]; // 正确
let x = 1; let y = 2; [x, y] = [y, x];
// 返回一个数组 function example() { return [1, 2, 3]; } let [a, b, c] = example(); // 返回一个对象 function example() { return { foo: 1, bar: 2 }; } let { foo, bar } = example();
// 参数是一组有次序的值 function f([x, y, z]) { ... } f([1, 2, 3]); // 参数是一组无次序的值 function f({x, y, z}) { ... } f({z: 3, y: 2, x: 1});
let jsonData = { id: 42, status: "OK", data: [867, 5309] }; let { id, status, data: number } = jsonData; console.log(id, status, number); // 42, "OK", [867, 5309]
const 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) { // ... }
const { SourceMapConsumer, SourceNode } = require("source-map");