Lost Javascript object constructor on prototype change

In the code below

var A = function() {};
var a = new A();
var b = new A();

A.prototype = {};

var c = new A();
console.log(a.constructor === b.constructor);
console.log(a.constructor === c.constructor);

conclusion trueand false.

I'm interested in a false conclusion. aand cwere created using the same constructor function, which a- why is their constructor property different? I think I missed something.

PS. If I remove the line where I change the prototype O A: true true.

+4
source share
2 answers

, constructor prototype __proto__, , a b ( .constructor), c ( constructor ). :

a = new A; b = new A:

enter image description here

A.prototype = {}; c = new A()

enter image description here

, JS ( ). .

+4

, A.prototype={} A.

A.prototype={}, constructor.

?

constructor prototype.

A.prototype Object {}, constructor ( Object).

:

function A() { } // (1)
A.prototype = {}  // (2)

(2)

Object.prototype
{
    constructor:Object
}

A.prototype
//was autocreated with A
{
    constructor:A
}

(2)

Object.prototype
{
    constructor:Object
}

A.prototype
//reassigned with {}
{
   //no constructor
}
+4

Source: https://habr.com/ru/post/1663208/


All Articles