Issue with overwriting prototype with object in javascript

Anarpneaw

New Member
Trying to understand prototypes. I'm playing around in Chrome's console and was hoping someone can point me to why this is happening.\[code\]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.prototypeGadget {price: 100, rating: 3, getInfo: function} //Expected\[/code\]Now if I try the following, prototype does not have the expected results.\[code\]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.prototypeObject {} //Empty Object!!!!!???\[/code\]
 
Back
Top