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

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Printed from: http://www.bowerpower.cn/archives/547 .
© bowerpower 2010.

Leave a Reply

*
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

  •  

    September 2010
    M T W T F S S
    « Aug    
     12345
    6789101112
    13141516171819
    20212223242526
    27282930