Is there a good use case for the constructor property in Javascript?

First, this question is not "what does a constructor property do?" - There is a lot of good documentation about what it is and how it works: This is a link to the function that created the object (which can be inherited from its prototype).

I am more interested in knowing common use cases for this property. Everything seems to be fine in theory, but when do you really need a reference to the function that built your object? A few ideas:

  • Maybe I want to clone it. I could call the constructor again and get another instance of my object. This, of course, will not work since you can potentially create an instance of your object as a prototype, and not the object itself; plus the preferred method is to create a new object and instead install this prototype of the object.
  • Perhaps you can use it to find out what the "type" of an object is. This seems rather strange since you can use instanceof or Object.prototype.toString() .
  • Can you change or reassign a constructor? Will there ever be a good reason to do this?

Hopefully some people can listen to some good Javascript paterns that use the constructor reference, or provide an official explanation of why the property exists.

+5
source share
3 answers

One case where a constructor property is convenient (or would be if it were reliable) is where the function should know the type of argument that it passed, for example.

 function foo(arg) { if ( /* if arg is an array */ ) { // deal with array } else if ( /* if arg is an object */ ) { // deal with object } } 

If an array or object is passed to the above function, then typeof will return the object in both cases. The constructor property can be used:

  if ( arg.constructor == Array ) 

But this does not work if the array is created in another frame where the test is running (i.e. its Array constructor is another object of the Array function in the test area).

So, if you exclude frames (or other cases where the area is a problem), then the constructor property is suitable for this.

But this does not exclude that the general problem of constructor properties is the ability to write (and therefore can be installed on anything) and cases where the prototype chain is more trivial.

+4
source

One good use case is to implement “inheritance” and “classes in javascript”. Consider the following example:

 // define the Person Class function Person() {} Person.prototype.walk = function(){ alert ('I am walking!'); }; Person.prototype.sayHello = function(){ alert ('hello'); }; // define the Student class function Student() { // Call the parent constructor Person.call(this); } // inherit Person Student.prototype = new Person(); // correct the constructor pointer because it points to Person Student.prototype.constructor = Student; // replace the sayHello method Student.prototype.sayHello = function(){ alert('hi, I am a student'); } // add sayGoodBye method Student.prototype.sayGoodBye = function(){ alert('goodBye'); } var student1 = new Student(); student1.sayHello(); student1.walk(); student1.sayGoodBye(); // check inheritance alert(student1 instanceof Person); // true alert(student1 instanceof Student); // true 

As you can see, we inherited Person by reassigning Student.prototype to new Person(); , but then we need to reassign the constructor back to Student, since we want to instantiate the Student class, but not Person.

UPDATE

Btw, a more realistic example of inheritance in javascript is to use an intermediate function, so the Person object will not be created at the definition stage:

 // inherit Person function F() {} F.prototype = Person.prototype; Student.prototype = new F(); 
+3
source

I used it before for type checking. It is not much more useful than instanceof , but it is a bit more explicit.

 function isArray(o) { return o.constructor == Array; } 
+1
source

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


All Articles