ES6解构

对象解构

1
2
3
4
5
6
7
const a = {id: 1, name: 'a1', age: 30};
const {id, ccc} = a;
const {name, ...ddd} = a;
console.log(id); // 1
console.log(ccc); // undefined
console.log(name); // a1
console.log(ddd); // {id: 1, age: 30}

数组解构

1
2
3
4
5
6
7
const b = [1, 3, 5, 7, 9];
const [, eee] = b;
const [, ...fff] = b;
const [, , ...ggg] = fff;
console.log(eee); // 3
console.log(fff); // [3, 5, 7, 9]
console.log(ggg); // [7, 9]
0%