Trying to understand prototypes. I play in the Chrome console and hope someone can tell me why this is happening.
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
Now, if I try the following, the prototype will not have the expected results.
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 {}
source share