K
K
Kerrik Sun2021-07-04 19:03:10
JavaScript
Kerrik Sun, 2021-07-04 19:03:10

Why do sometimes changes to the "parent" prototype affect changes to the "child" prototype, and sometimes not?

Why sometimes changes in the "parent" property prototypeof the constructor function affect the change in prototypethe instance object contained in __proto__, and sometimes not?

That is, here changes in the "parent" instance prototypedo not affect the "child" prototypeinstance in any way:

function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

var rabbit = new Rabbit();

Rabbit.prototype = {};

alert( rabbit.eats ); //true - все работает без изменений


And here there is a change in the "parent" property - the "child" property changes:
function Rabbit(name) {}
Rabbit.prototype = {
  eats: true
};

var rabbit = new Rabbit();

Rabbit.prototype.eats = false;

alert( rabbit.eats ); //false - происходят изменения


Why is this happening? What is the logic of such behavior?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question