if I just use obj.prototype.property = val, everything will be ok, code like
function animal() {}
var frog = new animal();
animal.prototype.color = 'Green';
console.log(frog.color);
// 'Green'
But if I use obj.prototype = {key:val}
after new keyword, it will give me a undefined
, code like
function animal() {}
var frog = new animal();
animal.prototype = {
color: 'Green'
};
console.log(frog.color);
// 'undefined' -- why?
And if I change the order which let the prototype before the new keyword, it will be ok, so weird and why? because we know an object’s prototype allows us adding properties to all instances of that object (even to the existing instances), right?
code like
function animal() {}
animal.prototype = {
color: 'Green'
};
var frog = new animal();
console.log(frog.color);
// 'Green'