基本概念
只要某種數據結構具有Iterator接口都可以采用解構賦值和for of循環 如:
1數組(有序)
2對象(無序)
3字符串
4類數組
5map數據結構
6Generator 函數 (function*)
基本用法
var a = 1
var b = 2
var c = 3
等同于
var [a, b, c] = [1, 2, 3]
只有當等號兩邊的書寫模式相同時,才能進行解構賦值
let [aa,[[bb],cc]] = [11,[[22],33]]
aa; //11
bb; //22
cc; //33
let [,,c] = [1,2,3];
c; //3 不完全解構
let [one,two,third] = [1,2];
one; //1
two; //2
third; //undefined 不完全解構
let [a,[b],c] = [1,[22,33],3];
a; //1
b; //22 注意不是22,33
c; //3
解構不成功的情況
let [foo] = 1 //報錯error
var [bar, foo] = [1] //報錯error
默認值
解構賦值允許制定默認值
let [foo = true] =[];
foo; //true
let [x, y='b']=['a'];
x; //a
y; //b
let [foo = 1] = [undefined]
foo ; //1
let [mo = 1] = [null]
mo ; //1
對象的解構賦值(無序)
對象的解構賦值與數組的解構賦值不同在于,數組的元素是按次序排列的,變量的取值由它的位置決定;而對象的屬性沒有次序,變量必須與屬性同名,才能取到正確的值。
var { a, b} = { a: "aaa", b: "bbb" };
a// "aaa"
b// "bbb"
var { c} = { a: "aaa", b: "bbb" };
c// undefined
如果左邊的變量名和右邊的屬性名不一致,必須寫成
var { a: d} = { a: "aaa", b: "bbb" };
d// "aaa"
let obj = { first: 'hello', last: 'world' };
let { first: f, last: l } = obj;
f // 'hello'
l // 'world'
對象的解構賦值要注意被賦值的變量,是key:value的value
對象的嵌套賦值
var obj = {
p: [
"Hello",
{ y: "World" }
]
};
var { p: [x, { y }] } = obj;
x // "Hello"
y // "World"
//這里被賦值的是obj.p里面的value值
對象解構賦值默認值的寫法, 對象的屬性值嚴格等于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 {x = 3} = {x: undefined};
x // 3 在設置默認值時undefined不可賦值
var {x = 3} = {x: null};
x // null 在設置默認值時null可以賦值,因為null類型為一個具體的值
如果一個變量已經聲明,要解構賦值,需要用圓括號括起來,否則會出現解析錯誤
var x;
{x} = {x: 1};// 報錯 JS運行機制把{x}當作一個代碼塊運行,出現語法錯誤
var x;
({x} = {x:1}) // x = 1 正確的寫法,和立即執行函數道理相似
字符串的解構賦值
字符串也可以解構賦值。這是因為此時,字符串被轉換成了一個類似數組的對象。
const [a, b, c, d, e] = 'hello';
a // "h"
b // "e"
c // "l"
d // "l"
e // "o"
類似數組的對象都有一個length屬性,因此還可以對這個屬性解構賦值。
let {length : len} = 'hello';
len // 5
同理數組也可以
let {length : len} = [1,2,3,4,5,6];
len // 5
數值和布爾值的解構賦值
解構賦值時,如果等號右邊是數值和布爾值,則會先轉為對象。
let {toString: s} = 123; // 把Number對象的toString賦值給s
s === Number.prototype.toString // true
let {toString: s} = true; // 把Boolean對象的toString賦值給s
s === Boolean.prototype.toString // true
函數參數的解構賦值
函數的參數也可以使用解構賦值。
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]
解構失敗的情況,函數move的參數指定默認值(0,0),是整體性的,不是為變量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]
解構賦值的便捷使用場景
- 交換變量的value
[x, y] = [y, x];
- 函數返回多個value
// 返回數組
function fn() {
return [1, 2, 3];
}
var [a, b, c] = fn();
// 返回對象
function fn() {
return {
foo: 1,
bar: 2
};
}
var { foo, bar } = fn();
- 函數參數的定義
// 參數是一組有次序的值
function f([x, y, z]) { ... }
f([1, 2, 3])
// 參數是一組無次序的值
function f({x, y, z}) { ... }
f({z: 3, y: 2, x: 1})
- 提取JSON數據
var json = {
id: 1,
status: "OK",
data: [a, b]
}
let { id, status, data: arr} = json;
console.log(id, status, arr)
// 1, OK, [867, 5309]
- 加載模塊的指定方法
const { SourceMapConsumer, SourceNode } = require("source-map");
// 按需引入 Element
import Vue from 'vue'
import { Button, Select } from 'element-ui' //解構賦值得到指定方法
- 遍歷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) {
// ...
}
- Generator函數的解構
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