javascript实例共享受继承于同一个实例

Posted by bower on June 23, 2010
javascript

分享给你的朋友->bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

pp= function(){

this.foo=[1,2,3];

}

pp.prototype.dothefoo = function(){

this.foo.pop();

}

aa=  function(){

….

}

aa.prototype=new pp();

bb=  function(){

….

}

bb.prototype = new pp();

var a = new aa();

var b = new bb();

a.dothefoo();

b.dothefoo(); //继承于pp

//这时候

a.foo=[1,2];

b.foo=[1,2];

//这不是我想到的,我想两个实例a,b同实操作于foo

//可以这样

pp= function(){

this.foo=[1,2,3];

}

pp.prototype.dothefoo = function(){

this.foo.pop();

}

var p = new pp();

aa=  function(){

….

}

aa.prototype=p;

bb=  function(){

….

}

bb.prototype =p;

var a = new aa();

var b = new bb();

a.dothefoo();

b.dothefoo(); //继承于pp

//这时候

a.foo=[1]

b.foo=[1]

//他们是同一个变量

分享到:人人网开心网腾讯微博新浪微博豆瓣分享腾讯空间百度搜藏腾讯书签

分享给你的朋友->bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word