I'm not sure why when I overwrite the prototype below with an object(Gadget.prototype = {
price: 100;}, only new instances of Gadget(theGadget) have access to the new properties.
But when extended(Gadget.prototype.price = 100) All instances have access.
function Gadget(name, color) {
this.name = name;
this.color = color;
this.brand = "Sony";
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
myGadget = new Gadget();
myGadget.brand;
//Gadget.prototype.price = 100;
Gadget.prototype = {
price: 100,
rating: 3,
};
myGadget.price;
theGadget = new Gadget();
theGadget.price
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…