let [foo, [[bar], baz]] = [1, [[2], 3]]; foo // 1 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 // []
var [foo] = []; var [bar, foo] = [1];
// 報錯 let [foo] = 1; let [foo] = false; let [foo] = NaN; let [foo] = undefined; let [foo] = null; let [foo] = {};
var [v1, v2, ..., vN ] = array; let [v1, v2, ..., vN ] = array; const [v1, v2, ..., vN ] = array;
let [x, y, z] = new Set(["a", "b", "c"]); x // "a"
function* fibs() { var a = 0; var b = 1; while (true) { yield a; [a, b] = [b, a + b]; } } var [first, second, third, fourth, fifth, sixth] = fibs(); sixth // 5
解構賦值容許指定默認值。node
var [foo = true] = []; foo // true [x, y = 'b'] = ['a']; // x='a', y='b' [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
var [x = 1] = [undefined]; x // 1 var [x = 1] = [null]; x // null
function f() { console.log('aaa'); } let [x = f()] = [1];
let x; if ([1][0] === undefined) { x = f(); } else { x = [1][0]; }
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
解構不只能夠用於數組,還能夠用於對象。ajax
var { foo, bar } = { foo: "aaa", bar: "bbb" }; foo // "aaa" bar // "bbb"
var { bar, foo } = { foo: "aaa", bar: "bbb" }; foo // "aaa" bar // "bbb" var { baz } = { foo: "aaa", bar: "bbb" }; baz // undefined
var { foo: baz } = { foo: 'aaa', bar: 'bbb' }; baz // "aaa" let obj = { first: 'hello', last: 'world' }; let { first: f, last: l } = obj; f // 'hello' l // 'world'
var { 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 foo; ({foo} = {foo: 1}); // 成功 let baz; ({bar: baz} = {bar: 1}); // 成功
var node = { loc: { start: { line: 1, column: 5 } } }; var { loc: { start: { line }} } = node; line // 1 loc // error: loc is undefined start // error: start is undefined
var {x = 3} = {}; x // 3 var {x, y = 5} = {x: 1}; x // 1 y // 5 var {x:y = 3} = {}; y // 3 var {x:y = 3} = {x: 5}; y // 5 var { message: msg = 'Something went wrong' } = {}; msg // "Something went wrong"
var {x = 3} = {x: undefined}; x // 3 var {x = 3} = {x: null}; x // null
// 報錯 var {foo: {bar}} = {baz: 'baz'};
// 錯誤的寫法 var x; {x} = {x: 1}; // SyntaxError: syntax error
// 正確的寫法 ({x} = {x: 1});
let { log, sin, cos } = Math;
json
var arr = [1, 2, 3]; var {0 : first, [arr.length - 1] : last} = arr; first // 1 last // 3
字符串也能夠解構賦值。這是由於此時,字符串被轉換成了一個相似數組的對象。數組
const [a, b, c, d, e] = 'hello'; a // "h" b // "e" c // "l" d // "l" e // "o"
let {length : len} = 'hello'; len // 5
解構賦值時,若是等號右邊是數值和布爾值,則會先轉爲對象。數據結構
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
函數的參數也可使用解構賦值。async
function add([x, y]){ return x + y; } add([1, 2]); // 3
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]
[1, undefined, 3].map((x = 'yes') => x); // [ 1, 'yes', 3 ]
( 1 )變量聲明語句中,不能帶有圓括號。函數
// 所有報錯 var [(a)] = [1]; var {x: (c)} = {}; var ({x: c}) = {}; var {(x: c)} = {}; var {(x): c} = {}; var { o: ({ p: p }) } = { o: { p: 2 } };
( 2 )函數參數中,模式不能帶有圓括號。ui
// 報錯 function f([(z)]) { return z; }
( 3 )賦值語句中,不能將整個模式,或嵌套模式中的一層,放在圓括號之中。url
// 所有報錯 ({ p: a }) = { p: 42 }; ([a]) = [5];
[(b)] = [3]; // 正確 ({ p: (d) } = {}); // 正確 [(parseInt.prop)] = [3]; // 正確
[x, y] = [y, x]; 交換變量x和y的值
// 返回一個數組 function example() { return [1, 2, 3]; } var [a, b, c] = example(); // 返回一個對象 function example() { return { foo: 1, bar: 2 }; } var { foo, bar } = example();
// 參數是一組有次序的值 function f([x, y, z]) { ... } f([1, 2, 3]); // 參數是一組無次序的值 function f({x, y, z}) { ... } f({z: 3, y: 2, x: 1});
var jsonData = { id: 42, status: "OK", data: [867, 5309] }; let { id, status, data: number } = jsonData; console.log(id, status, number); // 42, "OK", [867, 5309]
jQuery.ajax = function (url, { async = true, beforeSend = function () {}, cache = true, complete = function () {}, crossDomain = false, global = true, // ... more config }) { // ... do stuff };
任何部署了 Iterator 接口的對象,均可以用for...of循環遍歷。 Map 結構原生支持 Iterator 接口,配合變量的解構賦值,獲取鍵名和鍵值就很是方便。prototype
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) { // ... }
const { SourceMapConsumer, SourceNode } = require("source-map");
持續更新中~~~喜歡請留下個點個贊哦!