What is the use of constructor? you can print successfully without writing it.

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

        function Cat(name,color){
            this.name = name;
            this.color = color;
        }
        
        Cat.prototype = new Animal();
        Cat.prototype.constructor = Cat;

        var cat1 = new Cat("","");
        console.log(cat1.species)![][1]

Jun.18,2022

when you define a constructor

function Cat() {}

at this point, there is a constructor attribute on the prototype object of Cat , pointing to itself (that is, Cat )

.
Cat.prototype.constructor == Cat

when you execute the following code

Cat.prototype = new Animal();

you have modified prototype of Cat , and the prototype of Cat equals the instance of Animal , and the prototype object of Cat no longer has the attribute constructor , so you need to manually reassign the attribute Cat.prototype.constructor = Cat ,
of course you can't use the attribute constructor in general, so you can't reassign constructor .


is equivalent to a pointer in the C language that points to prevent confusion.


modify the constructor, otherwise the constructor of cat1 points to Animal , and there will be no inheritance


without this sentence Cat.prototype.constructor = Cat,cat.__proto__.constructor will point to Animal
, that is, the constructor function of cat is Animal, which is obviously wrong, although it can be used normally, but it does not conform to the inheritance logic.


finish execution

Cat.prototype = new Animal();  
//   Cat.prototype.constructor = Cat;

then run

Cat.prototype.constructor   // Animal();

Last

cat1.__proto__.constructor // Animal();

at this time, the constructor of the Cat.prototype / cat1.__proto__ prototype points to the Animal () constructor. If Cat.prototype.constructor/cat1.__proto__.constructor is used below, there will be an error. The intended Cat () constructor is actually calling the Animal () constructor

Menu