Javascript prototype problem

function Animal () {
this.species = "animal";
}

function Cat (name,color) {
this.name = name;
this.color = color;
}

The prototype of

Cat.prototype = new Animal ();
Cat.prototype.constructor = Cat;
/ / Cat points to the object generated by Animal. Don"t Cat and Animal
refer to the same prototype? if you overwrite the constructor of Cat, won"t it also overwrite the constructor of Animal? So why is the following sentence not false but
true

alert (Animal.prototype.constructor = = Animal); / / true

Dec.25,2021

Cat.prototype = new Animal ();: modify Cat.prototype to an instance of Animal

at this time, Cat.prototype.constructor is indeed equal to Animal

but there is no constructor attribute in Cat.prototype. Cat.prototype.constructor = Animal () just found Animal.prototype because the constructor attribute cannot be found in itself.

Cat.prototype.constructor = Cat; this step adds constructor familiarity to Cat.prototype, but does not affect Animal.prototype.constructor


does not refer to the same prototype, oh, first explain a small knowledge point:
We can access the prototype through the constructor .prototype , of course, you can also access the constructor through the prototype .constructor , so
Why Animal.prototype.constructor = Animal returns true believe you can already understand.

demo:

demo

but through the concept of prototype inheritance, we can know that after the prototype of Cat becomes Animal instance object , he can also inherit the method

in Animal prototype .
Menu