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)
At the time you’re calling
basically this assignment is done:
Then you reassign the
A.prototypeto a new object, socgets{}as its prototype.However, this doesn’t destroy the old
A.prototypeobject –a.__proto__is still pointing to it..constructoris basically just a convenience property. It has no effect on how instances behave.Not in my console, they don’t! (Opera 12)