待解構字段為原始值
正常情況下,
const obj = {
a: 1,
b: 2,
};
const { a, b } = obj;
console.log(a, b); // 1 2
當被解構字段缺失時,
const obj = {
a: 1,
};
const { a, b } = obj;
console.log(a, b); // 1 undefined
此時可在解構時使用 =
指定默認值:
const obj = {
a: 1,
};
const { a, b = 2 } = obj;
console.log(a, b); // 1 2
解構時指定別名
你甚至可以在解構字段的同時為其重命名,
const obj = {
a: 1,
b: undefined
}
const { a, b: c = 2 } = obj;
console.log(a, c) // 1 2
上述過程其實為:
- 創建變量
c
- 獲取
obj.b
並賦值給c
- 如果
obj.b
為undefined
,則將指定的默認值2
賦值給c
上面的過程等同於:
const c = obj.b || 2
待解構字段為對象
考察如下的對象:
const obj = {
innerObj: {
a: 1,
b: 2
}
}
正常情況下可通過如下的形式解構以得到內層的字段:
const obj = {
innerObj: {
a: 1,
b: 2,
},
};
const {
innerObj: { a, b = 2 },
} = obj;
console.log(a, b); // 1 2
但如果里面嵌套的對象缺失時,上面的解構會報錯:
const obj = {};
const {
innerObj: { a, b = 2 },
} = obj;
console.log(a, b); // 🚨 error: Uncaught TypeError: Cannot read property 'a' of undefined
此時需要在解構時對內層對象也指定默認值,形式如下:
const obj = {};
const {
innerObj: { a, b = 2 } = {},
} = obj;
console.log(a, b); // undefined 2
解構字段包含在多層嵌套內
當被解構字段包含在多層嵌套內時,甚至可以通過上面的方式為每一層都指定默認值:
const obj = {}
const { foo: { bar: { a, b = 2 } = {} } = {} } = obj;
console.log(a, b) // undefined 2
對象解構時需要注意,當其為 null
時,上述默認值並不生效,仍會報錯。具體見下方討論。
const obj = {
foo: {
bar: null
}
}
const { foo: { bar: { a, b = 2 } = {} } = {} } = obj;
console.log(a, b) // 🚨 error: Uncaught TypeError: Cannot destructure property 'a' of '{}' as it is null.
undefined
& null
上面討論的默認值僅在被解構字段的值為 undefined
時生效。拿被解構字段為原始為例,下面兩種情況默認值都會生效:
- 被解構字段缺失
const obj = {
a: 1,
};
const { a, b = 2 } = obj;
console.log(a, b); // 1 2
- 被解構字段顯式地擁有
undefined
值
const obj = {
a: 1
b: undefined
}
const { a, b = 2 } = obj;
console.log(a, b) // 1 2
但如果被解構字段的值為非 undefined
時,比如 null
,此時默認值並不生效,因為字段擁有 null
本身就是一種合法的值,所以再對其指定默認值便毫無意義。
於是,如下情況默認值不會生效:
const obj = {
a: 1
b: null
}
const { a, b = 2 } = obj;
console.log(a, b) // 1 null
這一規則在被解構字段為對象時同樣適用。
The text was updated successfully, but these errors were encountered: