Попытка понять прототипы. Я играю в консоли Chrome и надеюсь, что кто-то может указать мне, почему это происходит.Проблема с перезаписью прототипа с объектом в javascript
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
Gadget.prototype.price = 100;
Gadget.prototype.rating = 3;
Gadget.prototype.getInfo = function() {
return 'Rating: ' + this.rating + ', price: ' + this.price;
};
var newtoy = new Gadget('webcam', 'black');
newtoy.constructor.prototype
Gadget {price: 100, rating: 3, getInfo: function} //Expected
Теперь, если я пытаюсь следующий, прототип не имеет ожидаемых результатов.
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
Gadget.prototype = {
price: 100,
rating: 3,
getInfo: function() {
return 'Rating: ' + this.rating + ', price: ' + this.price;
}
};
var newtoy = new Gadget('webcam', 'black');
newtoy.constructor.prototype
Object {} //Empty Object!!!!!???
Вы ищете 'newtoy .__ proto__'? т.е. что вы ищете, проходя через 'newtoy.constructor'? –
http://stackoverflow.com/a/8096017/783743 –