Sunday, May 15, 2016

__proto__never_changes

var Foo = function Foo(){};
Foo.prototype.x = 1;
var FooInstance = new Foo();
console.log(FooInstance.x); // logs 1, as you think it would

//now let’s replace/override the prototype object with a new Object() object
Foo.prototype = {x:2};
console.log(FooInstance.x); // logs 1: once created, object's __proto__ never changes

// create a new instance of Foo()
var NewFooInstance = new Foo();
console.log(NewFooInstance.x); // logs 2

No comments:

Post a Comment