[ES6] Class 語法,和 Extend 延伸實例

首先依照這個資源,瞭解 ECMAScript 6 class

ECMAScript 6 入门:https://es6.ruanyifeng.com/#docs/class
Javascript 物件導向子類和父類的應用等
因此開啟了我的深入淺出論文?(又臭又長的文章XD)

我寫的範例:

這邊有jsbin連結

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class People {
constructor(height, money) {
this.height = height;
this.money = money;
}
work() {
console.log('I am working');
}
play() {
console.log('I am playing');
}
//info 可以先不用看
info() {
console.log('我身高',this.height, '我有',this.money);
}
}
//----------------特殊舉例-可以先不用理會-------------------
let cMan =new People ('0cm','0元');
//----------------aMan-object---------------------
class aMan extends People {
swim() {
console.log('aMan','I am swimming');
}
};
//----------------new-aMan------------------------
let poolMan = new aMan('165cm', '5億');
//----------------bMan-object---------------------
class bMan extends People {
constructor(height, money, face) {
super(height, money);
this.face = face;
}
swim() {
console.log('bMan','I am swimming');
}
talk() {
console.log('bMan','I am talking');
}
//info 可以先不用看
info() {
console.log('我身高有',this.height, '我錢有',this.money,'我臉很',this.face);
}
};
//----------------new-bMan------------------------
let richMan = new bMan('195cm','500億','帥');
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×