共计 2572 个字符,预计需要花费 7 分钟才能阅读完成。
依照肯定模式,从数组和对象中提取值,对变量进行赋值,称为解构赋值。
一、数组的解构赋值
let a = 1; | |
let b = 2; | |
let c = 3; | |
// 解构赋值的写法可间接写成上面这样:let [a,b,c] = [1,2,3] |
实质上这种写法属于模式匹配,只有等号两边的模式雷同,右边的变量就会被赋予对应的值。
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,上面的例子,foo 的值都为 undefined。
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] = {}; |
也可用于 Set 构造的数据类型
let [x, y, z] = new Set(['a', 'b', 'c']); | |
x // "a" |
事实上,只有某种数据结构具备 Iterator 接口,都能够采纳数组模式的解构赋值。
function* fibs() { | |
let a = 0; | |
let b = 1; | |
while (true) { | |
yield a; | |
[a, b] = [b, a + b]; | |
} | |
} | |
let [first, second, third, fourth, fifth, sixth] = fibs(); | |
sixth // 5 |
默认值 – 解构赋值容许指定默认值
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 [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: y is not defined |
二、对象的解构赋值
与数组解构赋值相似:
let {foo, bar} = {foo: 'aaa', bar: 'bbb'}; | |
foo // "aaa" | |
bar // "bbb" |
对象解构赋值与数组的区别:数组元素是按秩序排列的,变量的取值由它的地位决定,而对象的属性没有秩序,变量必须与属性同名,能力取到正确的值。若解构失败,变量的值为 undefined。
let {baz} = {foo: 'aaa', bar: 'bbb'}; | |
baz // undefined |
若变量名与属性名不统一,必须写成上面这种格局:
let {foo: name} = {foo: 'Liane', name: 'No.1'}; | |
console.log(name) //'Liane' | |
console.log(foo) // 报错,foo is not defined | |
let obj = {first: 'hello', last: 'world'}; | |
let {first: f, last: l} = obj; | |
f // 'hello' | |
l // 'world' |
由此可阐明,对象解构赋值是如下模式的简写:
即先找到同名属性,而后再赋给对应的变量,真正被赋值的是后者,而不是前者。
let {foo: foo, bar: bar} = {foo: 'aaa', bar: 'bbb'};
嵌套构造的解构赋值
let obj = { | |
p:[ | |
'Hello', | |
{y: 'World'} | |
] | |
} | |
let {P:[x,{y}]} = obj; | |
x //'Hello' | |
y //'World' |
解构赋值能够取到继承的属性。
const obj1 = {}; | |
const obj2 = {foo: 'bar'}; | |
Object.setPrototypeOf(obj1, obj2); | |
const {foo} = obj1; | |
foo // "bar" |
默认值:
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 | |
let {message: msg = 'Something went wrong'} = {}; | |
msg // "Something went wrong" | |
var {x = 3} = {x: undefined}; | |
x // 3 | |
var {x = 3} = {x: null}; | |
x // null |
留神:
1、如果要将一个曾经申明的变量用于解构赋值,须要在赋值语法外加上()
let x; | |
{x} = {x: 1};//SyntaxError: synjtax error | |
// 因 js 引擎会将 {x} 了解成一个代码块,从而产生语法错误 | |
// 正确写法:let x; | |
({x} = {x: 1}) |
2、因为数组实质是非凡的对象,因而能够对数组进行对象属性的解构
let arr = [1,2,3] | |
let {0: first, [arr.length-1]:last} = arr; | |
console.log(first) //1 | |
console.log(last) //3 |
三、字符串的解构赋值
const {a,b,c,d,e} = 'Hello'; | |
a //'H' | |
b //'e' | |
c //'l' | |
d //'l' | |
e //'o' | |
const {length: len} = 'Hello' | |
len //5 |
四、函数参数的解构赋值
const fun = ([x,y])=>{return x + y} | |
console.log(fun([1,2])) //3 | |
let arr = [[1,2],[3,4]].map(([a,b])=>a+b) | |
console.log(arr) //[3,7] |
正文完
发表至: javascript
2021-03-16