let person ={
name:"gaojian",
age:35
}
let {name:personName,age:personAge} =person;
console.log(personName) //gaojian
console.log(personAge) //35
可以简写:
let person ={
name:"gaojian",
age:35
}
let {name,age} =person;
console.log(name) //gaojian
console.log(age) //35
解构赋值不一定与对象的属性完全匹配,赋值时可以忽略某些属性,如果引用属性不存在则赋值undefined
let person ={
name:"gaojian",
age:35
}
let {name,job} = person
console.log(name) //gaojian
console.log(job) //undefined
也可以解构赋值时定义默认值 \如果有值 默认值会被覆盖
let person ={
name:"gaojian",
age:35
}
let {name,job="software"} = person
console.log(name) //gaojian
console.log(job) //software
如果事先声明的变量进行赋值,则赋值表达式必须包含在一对【括号】中
let person = {
name: "gaojian",
age: 35
}
let name, age;
({ name,age,job = "software"} = person)
console.log(name) //gaojian
console.log(age) //35
console.log(job) //software
一种奇怪的现象,如果只声明了一个name 后面的也会自动被声明;
let person = {
name: "gaojian",
age: 35,
job: {
title: "abc"
},
arr: [1, 2, 3, 4]
},
name;
({
name,
age,
job = "software",
arr
} = person)
console.log(name) //gaojian
console.log(age) //35
console.log(job) //{title: "abc"}
console.log(arr) // [1, 2, 3, 4]
嵌套解构 可以解构来复制对象属性
let person = {
name: "gaojian",
age: 35,
job: {
title: "abc"
}
}
let name, age;
({
name,
age,
job = "software"
} = person)
console.log(name) //gaojian
console.log(age) //35
console.log(job) //{title: "abc"}
let person = {
name: "gaojian",
age: 35,
job: {
title: "abc"
}
}
let name, age, job;
({
name,
age,
job = "software"
} = person)
person.job.title = "ssss" //因为是引用地址 新job 会被修改
console.log(name) //gaojian
console.log(age) //35
console.log(job) //{title: "ssss"}
可以进行匹配嵌套
let person = {
name: "gaojian",
age: 35,
job: {
title: "abc"
}
}
let name, age, job, mytitle;
({
name,
age,
job: {
title: mytitle
}
} = person)
person.job.title = "ssss"
console.log(name) //gaojian
console.log(age) //35
console.log(job); //undefined 这里会是undefined
console.log(mytitle) //abc 不会被修改
如果开始赋值成功而后的赋值出错,则只会完成一部分(使用try,catch)
let person = {
name: "gaojian",
age: 35,
job: {
title: "abc"
}
}
let name, age, job, bb, mytitle;
try {
({
name,
age,
foo: {
bar: bb
},
job: {
title: mytitle
}
} = person)
} catch (e) {
console.log(e);
}
person.job.title = "ssss"
//TypeError: Cannot read property 'bar' of undefined
console.log(name) //gaojian
console.log(age) //35
console.log(job); //undefined
console.log(mytitle) //undefined
函数参数解构
let person ={
name:"gaojian",
age:35
}
function func(foo,{name,age},bar){
console.log(foo,bar)
console.log(name,age)
}
//把对象传进去调用
func('1st',person,'sss') //1st gaojian 35 sss
数组的解构赋值
基本用法
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
。
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
如果等号的右边不是数组
(或者严格地说,不是可遍历的结构,参见《Iterator》一章),那么将会报错。
// 报错
let [foo] = 1;
let [foo] = false;
let [foo] = NaN;
let [foo] = undefined;
let [foo] = null;
let [foo] = {};
上面的语句都会报错,因为等号右边的值,要么转为对象以后不具备 Iterator 接口(前五个表达式),要么本身就不具备 Iterator 接口(最后一个表达式)。
Set 结构,也可以使用数组的解构赋值。
let [x, y, z] = new Set(['a', 'b', 'c']);
x // "a"
事实上,只要某种数据结构具有 Iterator 接口,都可以采用数组形式的解构赋值。
function*
关键字可以在表达式内部定义一个生成器函数 (不会)
```javascript const foo = function*() { yield ‘a’; yield ‘b’; yield ‘c’; };
let str = ‘’; for (const val of foo()) { str = str + val; }
console.log(str); // expected output: “abc”
- `yield*` 表达式迭代操作数,并产生它返回的每个值。
- `yield*` 表达式本身的值是当迭代器关闭时返回的值(即`done`为`true`时)`<br />`
```javascript
function* g1() {
yield 2;
yield 3;
yield 4;
}
function* g2() {
yield 1;
yield* g1();
yield 5;
}
var iterator = g2();
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: 4, done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
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
上面代码中,fibs
是一个 Generator 函数(参见《Generator 函数》一章),原生具有 Iterator 接口。解构赋值会依次从这个接口获取值。
默认值
解构赋值允许指定默认值。
let [foo = true] = [];
foo // true
let [x, y = 'b'] = ['a']; // x='a', y='b'
let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
注意,ES6 内部使用严格相等运算符(===
),判断一个位置是否有值。所以,如果一个数组成员不严格等于undefined
,默认值是不会生效的。
let [x = 1] = [undefined];
x // 1
let [x = 1] = [null];
x // null
上面代码中,如果一个数组成员是null
,默认值就不会生效,因为null
不严格等于undefined
。
如果默认值是一个表达式,那么这个表达式是惰性求值的,即只有在用到的时候,才会求值。
function f() {
console.log('aaa');
}
let [x = f()] = [1];
上面代码中,因为x
能取到值,所以函数f
根本不会执行。上面的代码其实等价于下面的代码。
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
上面最后一个表达式之所以会报错,是因为x
用到默认值y
时,y
还没有声明。