Possible Duplicate:
What it the significance of the Javascript constructor property?
In the Javascript docs at developer.mozilla.org, on the topic of inheritance there’s an example
// inherit Person
Student.prototype = new Person();
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
I wonder why should I update the prototype’s constructor property here?
Each function has a
prototypeproperty (even if you did not define it),prototypeobject has the only propertyconstructor(pointing to a function itself). Hence after you didStudent.prototype = new Person();constructorproperty ofprototypeis pointing onPersonfunction, so you need to reset it.You should not consider
prototype.constructoras something magical, it’s just a pointer to a function. Even if you skip lineStudent.prototype.constructor = Student;linenew Student();will work as it should.constructorproperty is useful e.g. in following situations (when you need to clone object but do not know exactly what function had created it):so it’s better to make sure
prototype.constructor()is correct.