In the section about inheritance in the MDN article Introduction to Object Oriented Javascript, I noticed they set the prototype.constructor:
// correct the
Yes, it is necessary. When you do
Student.prototype = new Person();
Student.prototype.constructor
becomes Person
. Therefore, calling Student()
would return an object created by Person
. If you then do
Student.prototype.constructor = Student;
Student.prototype.constructor
is reset back to Student
. Now when you call Student()
it executes Student
, which calls the parent constructor Parent()
, it returns the correctly inherited object. If you didn't reset Student.prototype.constructor
before calling it you would get an object that would not have any of the properties set in Student()
.