In JavaScript, every function’s prototype object has a non-enumerable property constructor which points to the function (EcmaScript §13.2). It is not used in any native functionality (e.g. instanceof checks only the prototype chain), however we are encouraged to adjust it when overwriting the prototype property of a function for inheritance:
SubClass.prototype = Object.create(SuperClass.prototype, {
constructor: {value:SubClass, writable:true, configurable:true}
});
But, do we (including me) do that only for clarity and neatness? Are there any real-world use cases that rely on the constructor property?
I can’t really see why the
constructorproperty is what it is in JS. I occasionally find myself using it to get to the prototypes of objects (like the Event object) in IE < 9. However I do think it’s there to allow some ppl to mimic classical OO programming constructs:So AFAIK, the “advantages” of the constructor property are: