-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest-05.js
63 lines (41 loc) · 1.02 KB
/
Test-05.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// ES5继承
function SubType(name, age){
SuperType.call(this, 'nanjing'); // 借用构造函数
this.name = name;
this.age= age;
}
SubType.prototype.sayHi = function(){
alert("hi");
}
function SuperType(address){
this.address = address;
}
SuperType.prototype.sayHello = function(){
alert("hello");
}
Object.setPrototypeOf(SubType.prototype, SuperType.prototype);
// ES6继承相当于
// Object.setPrototypeOf(SubType, SuperType);
// Object.setPrototypeOf(SubType.prototype, SubType.prototype);
class SuperType{
constructor(name){
this.name = name;
}
sayHi(){
console.log("hi there are SuperType");
}
}
class SubType extends SuperType{
constructor(name){
super(name);
this.age = 22;
}
sayHello(){
console.log("hello there are SubType");
}
}
let subType = new SubType('behippo');
subType.sayHi()
subType.sayHello();
// 区别ES5需要先建立子类的this,ES6在constructor中必需要先调用super(),子类是没有this的;
// ES6中的Class有两个继承链,可以继承类的静态方法