Why changing the prototype does not affect previously created objects?
javascript, prototype
Solution
At the time you're calling
var a = new A();
basically this assignment is done:
a.__proto__ = A.prototype;
Then you reassign the `A.prototype` to a new object, so `c` gets `{}` as its prototype.
A.prototype = {};
var c = new A();
However, this doesn't destroy the old `A.prototype` object - `a.__proto__` is still pointing to it.
Am i right that this was caused by the fact that a.constructor is not equal to c.constructor and each of them had own prototype?
`.constructor` is basically just a convenience property. It has no effect on how instances behave.
Extra question: why there were printed two undefined strings?
Not in my console, they don't! (Opera 12)
Problem
I have the following code: ``` var A = function() {}; var a = new A(); var b = new A(); A.prototype.member1 = 10; A.prototype = {} var c = new A(); console.log(a.member1); console.log(a.constructor === b.constructor); console.log(a.constructor === c.constructor); console.log('---------'); console.log(c.member1); ``` It's output is: ``` 10 true false --------- undefined undefined ``` The prototype of `a` and `b` has not changed and `c` had a new one. Am i right that this was caused by the fact that `a.constructor` is not equal to `c.constructor` and each of them had own `prototype`? Are there any other circs when constructors of two objects might not be equal? Extra question: why there were printed two `undefined` strings? (Chromium)