comments | Javascript source | Execution result in your current browser |
two object share the same prototype cal | function Circle(r) {
this.radius=r;
}
Circle.prototype.cal=function() {return this.radius*this.radius};
var circle1=new Circle(10);
document.write("circle1 "+circle1.cal());
var circle2=new Circle(11);
document.write("<br/>circle2 "+circle2.cal());
|
|
now, the circle1 provide its own cal function | circle1.cal=function() {return this.radius*this.radius/2};
//circle1 already has its own copy of cal function
document.write("circle1 "+circle1.cal());
//circle2 is still using prototype cal function
document.write("<br/>circle2 "+circle2.cal());
|
|
now, modify the prototype cal | Circle.prototype.cal=function() {return this.radius*this.radius/10};
document.write("circle1 "+circle1.cal());
//nothing change for circle2
document.write("<br/>circle2 "+circle2.cal());
|
|