In below code
var A = function() {};
var a = new A();
var b = new A();
A.prototype = {};
var c = new A();
console.log(a.constructor === b.constructor);
con
The problem is that when you use A.prototype={}
it is changed the behavior of A
object.
When you use A.prototype={}
, A object lost it’s constructor
.
Why it is this behavior ?
The constructor
property is assigned to prototype
function.
And in your case ,A.prototype
is replaced with a new Object {}
, which doesn't have constructor
property as own property (but its prototype has and is equal to Object
).
Lets see the following example:
function A() { } // (1)
A.prototype = {} // (2)
Before (2)
Object.prototype
{
constructor:Object
}
A.prototype
//was autocreated with A
{
constructor:A
}
After (2)
Object.prototype
{
constructor:Object
}
A.prototype
//reassigned with {}
{
//no constructor
}