您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

js原型对象使用的步奏详解

2025/1/11 3:18:48发布15次查看
这次给大家带来js原型对象使用的步奏详解,js原型对象使用的的注意事项有哪些,下面就是实战案例,一起来看一下。
我们先来一个简单的构造函数+原型对象的小程序
function createobj( uname, uage ) {       this.username = uname;       this.userage = uage;     }     createobj.prototype.showusername = function () {       return this.username;     }     createobj.prototype.showuserage = function () {       return this.userage;     }
这个程序,没有什么问题,但是非常的冗余,每次扩展一个方法,都要写一次原型对象,我们可以把原型对象prototype当做一个字面量对象,所有的方法都在字面量对象中扩展,可以达到同样的效果:
createobj.prototype = {       showuserage : function(){         return this.userage;       },       showusername : function(){         return this.username;       },     }     var obj1 = new createobj( 'ghostwu', 22 );     var obj2 = new createobj( '卫庄', 24 );     console.log( obj1.showusername(), obj1.showuserage() ); //ghostwu 22     console.log( obj2.showusername(), obj2.showuserage() ); //卫庄 24
但是这种原型(prototype)对象的写法,属于重写了createobj的默认原型对象,造成的第一个问题就是constructor不再指向createobj.
没有重写之前,constructor指向createobj
function createobj( uname, uage ) {       this.username = uname;       this.userage = uage;     }     createobj.prototype.showusername = function () {       return this.username;     }     createobj.prototype.showuserage = function () {       return this.userage;     }     console.log( createobj.prototype.constructor === createobj ); //true
重写之后,constructor指向object
createobj.prototype = {       showuserage : function(){         return this.userage;       },       showusername : function(){         return this.username;       },     }     console.log( createobj.prototype.constructor === createobj ); //false     console.log( createobj.prototype.constructor === object ); //true
所以说,constructor不能准确的标识对象,因为他会被修改
我们之前写的程序,基本都是在原型对象(prototype)上扩展完了方法之后,再实例化对象,我们看下,先实例化对象,再在原型对象(prototype)上扩展函数,
实例对象是否能正常的调用到扩展的方法?
function createobj( uname, uage ) {       this.username = uname;       this.userage = uage;     }     var obj1 = new createobj( 'ghostwu', 22 );     createobj.prototype.showusername = function(){       return this.username;     }     console.log( obj1.showusername() ); //ghostwu
可以正常调用,但是,如果原型对象是重写的,就调用不到了
function createobj( uname, uage ) {       this.username = uname;       this.userage = uage;     }     var obj1 = new createobj( 'ghostwu', 22 );     createobj.prototype = {       showusername : function(){         return this.username;       }     }     console.log( obj1.showusername() ); //报错
因为重写了原型对象之后,同时实例化又是在重写之前发生的,所以实例的隐式原型proto不会指向重写的原型对象,所以就调用不到另一个问题,如果在原型对象(prototype)上有引用类型,千万小心,因为多个实例共用原型对象,只要有一个实例改变了引用类型的值,其他实例全部会收到改变之后的结果。
function createobj(){}     createobj.prototype = {       name : 'ghostwu',       skills : [ 'php', 'javascript', 'linux' ]     };     var obj1 = new createobj();     obj1.skills.push( 'python' );     var obj2 = new createobj();     console.log( obj2.skills ); //'php', 'javascript', 'linux', 'python'
原型对象(prototype)的共享特性,可以很方便的为一些内置的对象扩展一些方法,比如,数组去重复
array.prototype.unique = function(){       var res = [];       for( var i = 0, len = this.length; i < len; i++ ){         if( res.indexof( this[i] ) == -1 ) {           res.push( this[i] );          }       }       return res;     }     var arr = [ 10, 20, 30, 20, 30, 20, 40, 20 ];     console.log( arr.unique() ); //10, 20, 30, 40
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
js设置元素样式步奏详解
html+canvas实现屏幕截取
jquery实现计时器功能详解
以上就是js原型对象使用的步奏详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product