6.ES6 简化对象写法

 

ES6 简化对象写法

ES6 允许在大括号里面,直接写入变量和函数,作为对象的属性和方法。这样的书写更加简洁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
let id = 1;
let name = "xiaoming";
let age = 10;
let study = function() {
console.log("studying");
}

const student = {
id,
name,
age,
study,
rest() {
console.log("have a rest");
}
};

console.log(student);
/**
* age: 10
* id: 1
* name: "xiaoming"
* rest: ƒ rest()
* study: ƒ ()
*/

let {rest} = student;
rest();
// have a rest