I have two instances of a class. Setting a property in the first instance will set it for the second instance too. Why? It shouldn't. The property is not a "prototype property".
Check the code below, what happens to Peter Griffin? Two objects are created and given the name "Peter Griffin" and "Roger Moore" but the alert boxes will say "Peter Moore" and "Roger Moore". What happened to Peter Griffin?
var BaseClass = function(){
this.name = "";
this.data = {};
this.data.lastname = "";
}
var ExtendedClass = function(){
this.setName = function(fname, lname){
this.name = fname;
this.data.lastname = lname;
}
this.hello = function(){
alert("Full name: " + this.name + " " + this.data.lastname);
}
}
ExtendedClass.prototype = new BaseClass();
pilot = new ExtendedClass();
driver = new ExtendedClass();
pilot.setName("Peter", "Griffin");
driver.setName("Roger", "Moore");
pilot.hello(); // Full name: Peter Moore
driver.hello(); // Full name: Roger Moore