js basic review 3-constructor

In the following code, we can see that the prototype object of prototypeRabbit and the object prototype of Rabbit instance object r1 r1.__proto__have an constructorattribute:
Insert picture description here

In many cases, we need to manually use the constructor attribute to point back to the original constructor. For example, Rabbitthere is only one method in the constructor just now sing. If we want to put multiple methods into Rabbitit, such as the following:

Insert picture description here
At this time, we have Rabbittwo methods inside, but! If we have many, many methods to put in Rabbitit, can we consider wrapping all these methods in one object?

Insert picture description here
The structure is much clearer in this way, but let's see what the consequences are.
Let's renew a new r1, and then see what the r1.__proto__sum Rabbit.prototypelooks like at this time :
Insert picture description here
OMG! ! They no longer point to the original Rabbit constructor! !
This is certainly not, this is the case r1.__proto__and Rabbit.prototypecan not find their creators (who is also the Rabbit constructor), so weManually refer back to the constructor:
Insert picture description here
That's it!

Guess you like

Origin blog.csdn.net/dyw3390199/article/details/114957605