# ❓ ES5 实现 ES6 class extends 过程
function inherit(sub, Super) {
sub.prototype = Object.create(Super.prototype, {
constructor: {
value: Sub,
enumerable: false, //不能枚举
writable: true,
configurable: true,
},
})
}
function Super(name) {
this.name = name
}
Super.prototype.play = function () {
console.log(`I like play games`)
}
function Sub(name, age) {
Super.call(this, name)
this.age = age
}
inherit(Sub, Super)
let child = new Sub('jeff', 18)
Sub.prototype.say = function () {
console.log(`I am ${this.name}, ${this.age} years`)
}
Sub.prototype.work = function () {
console.log(`I like Coding`)
}
// child.play()
child.say()
let child2 = new Sub('rose', 16)
// child2.work()
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
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