File tree Expand file tree Collapse file tree 1 file changed +26
-9
lines changed
Expand file tree Collapse file tree 1 file changed +26
-9
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments