In the section about inheritance in the MDN article Introduction to Object Oriented Javascript, I noticed they set the prototype.constructor:
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
Does this serve any important purpose? Is it okay to omit it?
It’s not always necessary, but it does have its uses. Suppose we wanted to make a copy method on the base
Personclass. Like this:Now what happens when we create a new
Studentand copy it?The copy is not an instance of
Student. This is because (without explicit checks), we’d have no way to return aStudentcopy from the “base” class. We can only return aPerson. However, if we had reset the constructor:…then everything works as expected: