JavaScript loop prototype constructor

I worked on a blog on the constructor property of a function object and came across this line:

The prototype property of the Object function has a constructor property set by the function itself

Value, next function object

 function Student(name,age) { this.name = name; this.age = age; } 

will have the following prototype

{constructor : Student}

  function Student() { } console.log(Student.prototype); console.log(Student.prototype.constructor); console.log(Student.prototype.constructor.prototype); console.log(Student.prototype.constructor.prototype.constructor); console.log(Student.prototype.constructor.prototype.constructor.prototype); 

which means that the prototype has a constructor property, which is set by the function itself, which has the same prototype object. It has some reasons, or it was just a feature of the language. I could not find any reason to have a circular link here.

Any help appreciated. Thanks.

+1
source share
2 answers

The prototype.constructor property is referenced by a constructor function so that objects created by this constructor can check which constructor function is used to create them.

0
source

Yes, this is just a function of the language. And quite useful:

  • The .prototype reference to the constructor is used to configure inheritance when creating a new instance with new
  • the .constructor link on the prototype can be used to access the "class" from the instance, for example. to create a clone, to reinitialize the instance, to access the class name, or simply to check the value.
0
source

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


All Articles