X Tutup
Skip to content

Commit b6051aa

Browse files
committed
update
1 parent 6d7a692 commit b6051aa

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

OO/createObject.html

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,33 @@
155155

156156

157157
//【动态原型模式】
158-
// function Person(name, age) {
159-
// this.name = name;
160-
// this.age = age;
158+
function Person(name, age) {
159+
this.name = name;
160+
this.age = age;
161161

162-
// if (typeof this.sayName != 'function') {
163-
// Person.prototype.sayName = function() {
164-
// console.log(this.name);
165-
// }
166-
// }
167-
// }
162+
if (typeof this.sayName == 'function') return;
163+
// console.log('create');
164+
Person.prototype.sayName = function() {
165+
console.log(this.name);
166+
}
167+
168+
Person.prototype.sayAge = function() {
169+
console.log(this.age);
170+
}
171+
}
172+
173+
var person1 = new Person('xyx', 123),
174+
person2 = new Person('yyy', 456);
175+
176+
console.log(person1);
177+
console.log(person2);
178+
179+
person1.sayName();
180+
person2.sayAge();
181+
182+
//动态原型模式问题
183+
//动态原型模式实际是是对组合模式进行了一次包装,将对原型对象的初始化操作放入构造函数内部了。
184+
//对原型方法存在的验证可以避免对原型对象多次初始化。
168185
//【动态原型模式】
169186

170187

0 commit comments

Comments
 (0)
X Tutup