|
73 | 73 |
|
74 | 74 |
|
75 | 75 | //【组合继承(伪经典继承)】 |
76 | | -// function SuperType(name) { |
77 | | -// this.name = name; |
78 | | -// this.colors = ['red', 'blue', 'green']; |
79 | | -// } |
| 76 | +function SuperType(name) { |
| 77 | + this.name = name; |
| 78 | + this.colors = ['red', 'blue', 'green']; |
| 79 | +} |
80 | 80 |
|
81 | | -// SuperType.prototype.sayName = function() { |
82 | | -// console.log(this.name); |
83 | | -// }; |
| 81 | +SuperType.prototype.sayName = function() { |
| 82 | + console.log(this.name); |
| 83 | +}; |
84 | 84 |
|
85 | | -// function SubType(name, age) { |
86 | | -// SuperType.call(this, name); |
87 | | -// this.age = age; |
88 | | -// } |
| 85 | +function SubType(name, age) { |
| 86 | + SuperType.call(this, name); //第二次调用 SuperType() |
| 87 | + this.age = age; |
| 88 | +} |
89 | 89 |
|
90 | | -// SubType.prototype = new SuperType(); |
| 90 | +SubType.prototype = new SuperType(); //第一次调用 SuperType() |
91 | 91 | // console.log(SubType.prototype.constructor); //SuperType |
92 | | -// SubType.prototype.constructor = SubType; |
93 | | -// SubType.prototype.sayAge = function() { |
94 | | -// console.log(this.age); |
95 | | -// }; |
| 92 | +SubType.prototype.constructor = SubType; |
| 93 | +SubType.prototype.sayAge = function() { |
| 94 | + console.log(this.age); |
| 95 | +}; |
96 | 96 |
|
97 | | -// var instanceA = new SubType('AAA', 29); |
98 | | -// instanceA.colors.push('black'); |
99 | | -// console.log(instanceA.colors); |
100 | | -// instanceA.sayName(); |
101 | | -// instanceA.sayAge(); |
| 97 | +var instanceA = new SubType('AAA', 29); |
| 98 | +instanceA.colors.push('black'); |
| 99 | +console.log(instanceA.colors); |
| 100 | +instanceA.sayName(); |
| 101 | +instanceA.sayAge(); |
102 | 102 |
|
103 | | -// var instanceB = new SubType('BBB', 27); |
104 | | -// console.log(instanceB.colors); |
105 | | -// instanceB.sayName(); |
106 | | -// instanceB.sayAge(); |
| 103 | +var instanceB = new SubType('BBB', 27); |
| 104 | +console.log(instanceB.colors); |
| 105 | +instanceB.sayName(); |
| 106 | +instanceB.sayAge(); |
107 | 107 |
|
108 | 108 | //组合继承问题 |
109 | 109 | //组合继承融合了原型链与借用构造函数的优点,避免了二者的缺陷,是最常用的继承模式。 |
| 110 | +//由于调用了两次 SuperType,会分别在 SubType 的原型对象和实例对象上分别创建属性。 |
| 111 | + |
| 112 | +console.log(instanceA); //instanceA.name & instanceA.__proto__.name |
110 | 113 |
|
111 | 114 | //【组合继承】 |
112 | 115 |
|
|
178 | 181 | //【寄生式继承】 |
179 | 182 |
|
180 | 183 | //【寄生组合式继承】 |
181 | | -function inheritPrototype(subType, superType) { |
182 | | - var prototype = Object.create(superType.prototype); |
183 | | - prototype.constructor = subType; |
184 | | - subType.prototype = prototype; |
185 | | -} |
| 184 | +// function inheritPrototype(subType, superType) { |
| 185 | +// var prototype = Object.create(superType.prototype); |
| 186 | +// prototype.constructor = subType; |
| 187 | +// subType.prototype = prototype; |
| 188 | +// } |
186 | 189 |
|
187 | | -function SuperType(name) { |
188 | | - this.name = name; |
189 | | - this.colors = ['red', 'blue', 'green']; |
190 | | -} |
| 190 | +// function SuperType(name) { |
| 191 | +// this.name = name; |
| 192 | +// this.colors = ['red', 'blue', 'green']; |
| 193 | +// } |
191 | 194 |
|
192 | | -SuperType.prototype.sayName = function() { |
193 | | - console.log(this.name); |
194 | | -}; |
| 195 | +// SuperType.prototype.sayName = function() { |
| 196 | +// console.log(this.name); |
| 197 | +// }; |
195 | 198 |
|
196 | | -function SubType(name, age) { |
197 | | - SubType.call(this, name); |
198 | | - this.age = age; |
199 | | -} |
| 199 | +// function SubType(name, age) { |
| 200 | +// SuperType.call(this, name); |
| 201 | +// this.age = age; |
| 202 | +// } |
200 | 203 |
|
201 | | -inheritPrototype(SubType, SuperType); |
| 204 | +// inheritPrototype(SubType, SuperType); |
| 205 | + |
| 206 | +// SubType.prototype.sayAge = function() { |
| 207 | +// console.log(this.age); |
| 208 | +// }; |
| 209 | + |
| 210 | +// var instanceA = new SubType('aaa', 26); |
| 211 | + |
| 212 | +// console.log(instanceA); |
| 213 | + |
| 214 | +//寄生组合式继承问题 |
| 215 | +//上例的高效率体现在它只调用了一次 SuperType 构造函数,并且因此避免了在 SubType.prototype 上创建不必要的属性。寄生组合式继承是引用类型最理想的继承范式。 |
202 | 216 |
|
203 | | -SubType.prototype.sayAge = function() { |
204 | | - console.log(this.age); |
205 | | -}; |
206 | 217 | //【寄生组合式继承】 |
207 | 218 |
|
208 | 219 | </script> |
|
0 commit comments