在类里面可以去定义一些getter和setter,getter可以得到一些东西的方法,setter可以设置东西
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
|
class Chef{ constructor(food){ this .food = food; thid.dish = []; } //getter get menu(){ return this .dish } //setter set menu(dish){ this .dish.push(dish) } cook(){ console.log( this .food) } } let zhangsan = new Chef(); console.log(zhangsan.menu = 'tomato' ); //tomato console.log(zhangsan.menu = 'pizza' ); //pizza console.log(zhangsan.menu); //["tomato","pizza"] |