X Tutup
Skip to content

Commit 7884158

Browse files
committed
update
1 parent 87e0d9d commit 7884158

File tree

1 file changed

+56
-45
lines changed

1 file changed

+56
-45
lines changed

OO/inherit.html

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,43 @@
7373

7474

7575
//【组合继承(伪经典继承)】
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+
}
8080

81-
// SuperType.prototype.sayName = function() {
82-
// console.log(this.name);
83-
// };
81+
SuperType.prototype.sayName = function() {
82+
console.log(this.name);
83+
};
8484

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+
}
8989

90-
// SubType.prototype = new SuperType();
90+
SubType.prototype = new SuperType(); //第一次调用 SuperType()
9191
// 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+
};
9696

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();
102102

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();
107107

108108
//组合继承问题
109109
//组合继承融合了原型链与借用构造函数的优点,避免了二者的缺陷,是最常用的继承模式。
110+
//由于调用了两次 SuperType,会分别在 SubType 的原型对象和实例对象上分别创建属性。
111+
112+
console.log(instanceA); //instanceA.name & instanceA.__proto__.name
110113

111114
//【组合继承】
112115

@@ -178,31 +181,39 @@
178181
//【寄生式继承】
179182

180183
//【寄生组合式继承】
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+
// }
186189

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+
// }
191194

192-
SuperType.prototype.sayName = function() {
193-
console.log(this.name);
194-
};
195+
// SuperType.prototype.sayName = function() {
196+
// console.log(this.name);
197+
// };
195198

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+
// }
200203

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 上创建不必要的属性。寄生组合式继承是引用类型最理想的继承范式。
202216

203-
SubType.prototype.sayAge = function() {
204-
console.log(this.age);
205-
};
206217
//【寄生组合式继承】
207218

208219
</script>

0 commit comments

Comments
 (0)
X Tutup