经常看到有人在写js代码时,写成面向对象的形式,如:
代码1function MyObject(sName){ this.name=sName; this.MessageBox=function(){ alert("名字"+ this.name); } this.ChangeName=ChangeName; function ChangeName(){ this.name="改变" + this.name; }}代码2function MyObject(sName){ this.name=sName;}MyObject.prototype.MessageBox=function(){ alert("名字"+ this.name);}MyObject.prototype.ChangeName=function(){ this.name="改变" + this.name;}始终想不明白为什么要这么写,有人说是为了继承,是为了重用,我觉得,如果想重用js代码,直接在重用的地方调用一下不就可以了吗/?为什么要写这样的形式?有什么好处?感觉不适应!
代码1function MyObject(sName){ this.name=sName; this.MessageBox=function(){ alert("名字"+ this.name); } this.ChangeName=ChangeName; function ChangeName(){ this.name="改变" + this.name; }}代码2function MyObject(sName){ this.name=sName;}MyObject.prototype.MessageBox=function(){ alert("名字"+ this.name);}MyObject.prototype.ChangeName=function(){ this.name="改变" + this.name;}始终想不明白为什么要这么写,有人说是为了继承,是为了重用,我觉得,如果想重用js代码,直接在重用的地方调用一下不就可以了吗/?为什么要写这样的形式?有什么好处?感觉不适应!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货