1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < title > JavaScript继承</ title >
7
+ </ head >
8
+
9
+ < body >
10
+ < script >
11
+ // 原型链继承
12
+ // function Person(name, age) {
13
+ // this.name = name,
14
+ // this.age = age,
15
+ // this.setName = function () { }
16
+ // }
17
+ // Person.prototype.setAge = function () { }
18
+ // function Student(name, age, price) {
19
+ // Person.call(this, name, age)
20
+ // this.price = price
21
+ // }
22
+ // var s1 = new Student('Tom', 20, 15000)
23
+ // console.log(s1)
24
+
25
+ // 借用构造函数继承
26
+ // function Person(name, age) {
27
+ // this.name = name,
28
+ // this.age = age
29
+ // }
30
+ // Person.prototype.setAge = function () {
31
+ // console.log("111")
32
+ // }
33
+ // function Student(price) {
34
+ // this.price = price
35
+ // this.setScore = function () { }
36
+ // }
37
+ // Student.prototype.sayHello = function () { }
38
+ // Student.prototype = new Person
39
+ // Student.prototype.sayHello = function () { }
40
+ // var s1 = new Student(15000)
41
+ // var s2 = new Student(14000)
42
+ // console.log(s1, s2)
43
+ // s1.play.push(4)
44
+ // console.log(s1.setAge, s2.setAge)
45
+ // console.log(s1.__proto__ === s2.__proto__)
46
+ // console.log(s1.__proto__.__proto__ === s2.__proto__.__proto__)
47
+ // console.log(s1.__proto__.__proto__.__proto__ === Object.prototype)
48
+
49
+ // 原型链+借用构造函数的组合继承
50
+ // function Person(name, age) {
51
+ // this.name = name,
52
+ // this.age = age,
53
+ // this.setAge = function () { }
54
+ // }
55
+ // Person.prototype.setAge = function () {
56
+ // console.log("111")
57
+ // }
58
+ // var p1 = new Person('jack', 15)
59
+ // function Student(name, age, price) {
60
+ // Person.call(this, name, age)
61
+ // this.price = price
62
+ // this.setScore = function () { }
63
+ // }
64
+ // Student.prototype = new Person()
65
+ // Student.prototype.constructor = Student//组合继承也是需要修复构造函数指向的
66
+ // Student.prototype.sayHello = function () { }
67
+ // var s1 = new Student('Tom', 20, 15000)
68
+ // var s2 = new Student('Jack', 22, 14000)
69
+ // console.log(s1.constructor) //Student
70
+ // console.log(p1.constructor) //Person
71
+
72
+ // 组合继承优化1
73
+ // function Person(name, age) {
74
+ // this.name = name,
75
+ // this.age = age,
76
+ // this.setAge = function () { }
77
+ // }
78
+ // Person.prototype.setAge = function () {
79
+ // console.log("111")
80
+ // }
81
+ // function Student(name, age, price) {
82
+ // Person.call(this, name, age)
83
+ // this.price = price
84
+ // this.setScore = function () { }
85
+ // }
86
+ // Student.prototype = Person.prototype
87
+ // Student.prototype.sayHello = function () { }
88
+ // var s1 = new Student('Tom', 20, 15000)
89
+ // console.log(s1)
90
+ // console.log(s1 instanceof Student, s1 instanceof Person)//true true
91
+ // console.log(s1.constructor)//Person
92
+
93
+ //组合继承优化2
94
+ function Person ( name , age ) {
95
+ this . name = name ,
96
+ this . age = age
97
+ }
98
+ Person . prototype . setAge = function ( ) {
99
+ console . log ( "111" )
100
+ }
101
+
102
+ function Student ( name , age , price ) {
103
+ Person . call ( this , name , age )
104
+ this . price = price
105
+ this . setScore = function ( ) { }
106
+ }
107
+ Student . prototype = Object . create ( Person . prototype )
108
+ Student . prototype . constructor = Student
109
+ var s1 = new Student ( 'Tom' , 20 , 15000 )
110
+ console . log ( s1 instanceof Student , s1 instanceof Person ) // true true
111
+ console . log ( s1 . constructor ) //Student
112
+ console . log ( s1 )
113
+
114
+ //ES6 class继承
115
+ // class Person {
116
+ // //调用类的构造方法
117
+ // constructor(name, age) {
118
+ // this.name = name
119
+ // this.age = age
120
+ // }
121
+ // //定义一般的方法
122
+ // showName() {
123
+ // console.log("调用父类的方法")
124
+ // console.log(this.name, this.age);
125
+ // }
126
+ // }
127
+ // let p1 = new Person('kobe', 39)
128
+ // console.log(p1)
129
+ // //定义一个子类
130
+ // class Student extends Person {
131
+ // constructor(name, age, salary) {
132
+ // super(name, age)
133
+ // this.salary = salary
134
+ // }
135
+ // showName() { //在子类自身定义方法
136
+ // console.log("调用子类的方法")
137
+ // console.log(this.name, this.age, this.salary);
138
+ // }
139
+ // }
140
+ // let s1 = new Student('wade', 38, 1000000000)
141
+ // let s2 = new Student('kobe', 40, 3000000000)
142
+ // console.log(s1.showName === s2.showName)//true
143
+ // console.log(s1)
144
+ // s1.showName()
145
+ </ script >
146
+ </ body >
147
+
148
+ </ html >
0 commit comments