[js高手之路]使用原型对象(prototype)需要注意的地方

我们先来一个简单的构造函数+原型对象的小程序

 1         function CreateObj( uName, uAge ) {
 2             this.userName = uName;
 3             this.userAge = uAge;
 4         }
 5         CreateObj.prototype.showUserName = function () {
 6             return this.userName;
 7         }
 8         CreateObj.prototype.showUserAge = function () {
 9             return this.userAge;
10         }

这个程序,没有什么问题,但是非常的冗余,每次扩展一个方法,都要写一次原型对象,我们可以把原型对象prototype当做一个字面量对象,所有的方法都在字面

量对象中扩展,可以达到同样的效果:

 1         CreateObj.prototype = {
 2             showUserAge : function(){
 3                 return this.userAge;
 4             },
 5             showUserName : function(){
 6                 return this.userName;
 7             },
 8         }
 9         var obj1 = new CreateObj( 'ghostwu', 22 );
10         var obj2 = new CreateObj( '卫庄', 24 );
11         console.log( obj1.showUserName(), obj1.showUserAge() ); //ghostwu 22
12         console.log( obj2.showUserName(), obj2.showUserAge() ); //卫庄 24

但是这种原型(prototype)对象的写法,属于重写了CreateObj的默认原型对象,造成的第一个问题就是constructor不再指向CreateObj.

没有重写之前,constructor指向CreateObj

 1         function CreateObj( uName, uAge ) {
 2             this.userName = uName;
 3             this.userAge = uAge;
 4         }
 5         CreateObj.prototype.showUserName = function () {
 6             return this.userName;
 7         }
 8         CreateObj.prototype.showUserAge = function () {
 9             return this.userAge;
10         }
11         console.log( CreateObj.prototype.constructor === CreateObj ); //true

重写之后,constructor指向Object

 1         CreateObj.prototype = {
 2             showUserAge : function(){
 3                 return this.userAge;
 4             },
 5             showUserName : function(){
 6                 return this.userName;
 7             },
 8         }
 9         console.log( CreateObj.prototype.constructor === CreateObj ); //false
10         console.log( CreateObj.prototype.constructor === Object ); //true

所以说,constructor不能准确的标识对象,因为他会被修改

我们之前写的程序,基本都是在原型对象(prototype)上扩展完了方法之后,再实例化对象,我们看下,先实例化对象,再在原型对象(prototype)上扩展函数,

实例对象是否能正常的调用到扩展的方法?

1         function CreateObj( uName, uAge ) {
2             this.userName = uName;
3             this.userAge = uAge;
4         }
5         var obj1 = new CreateObj( 'ghostwu', 22 );
6         CreateObj.prototype.showUserName = function(){
7             return this.userName;
8         }
9         console.log( obj1.showUserName() ); //ghostwu

可以正常调用,但是,如果原型对象是重写的,就调用不到了

 1         function CreateObj( uName, uAge ) {
 2             this.userName = uName;
 3             this.userAge = uAge;
 4         }
 5         var obj1 = new CreateObj( 'ghostwu', 22 );
 6         CreateObj.prototype = {
 7             showUserName : function(){
 8                 return this.userName;
 9             }
10         }
11         console.log( obj1.showUserName() ); //报错

因为重写了原型对象之后,同时实例化又是在重写之前发生的,所以实例的隐式原型__proto__不会指向重写的原型对象,所以就调用不到

另一个问题,如果在原型对象(prototype)上有引用类型,千万小心,因为多个实例共用原型对象,只要有一个实例改变了引用类型的值,其他实例全部会收到

改变之后的结果

1         function CreateObj(){}
2         CreateObj.prototype = {
3             name : 'ghostwu',
4             skills : [ 'php', 'javascript', 'linux' ]
5         };
6         var obj1 = new CreateObj();
7         obj1.skills.push( 'python' );
8         var obj2 = new CreateObj();
9         console.log( obj2.skills ); //'php', 'javascript', 'linux', 'python'

原型对象(prototype)的共享特性,可以很方便的为一些内置的对象扩展一些方法,比如,数组去重复

 1         Array.prototype.unique = function(){
 2             var res = [];
 3             for( var i = 0, len = this.length; i < len; i++ ){
 4                 if( res.indexOf( this[i] ) == -1 ) {
 5                    res.push( this[i] ); 
 6                 }
 7             }
 8             return res;
 9         }
10         var arr = [ 10, 20, 30, 20, 30, 20, 40, 20 ];
11         console.log( arr.unique() ); //10, 20, 30, 40

但是,不要随便往内置的对象上面扩展方法,在多人协作开发,很容易产生覆盖,以及污染.

posted @ 2017-08-26 19:45  ghostwu  阅读(1612)  评论(3编辑  收藏  举报
Copyright ©2017 ghostwu