When making the prototypal inheritance, it’s asked to refer the child’s constructor back to itself below,
A = function() {}
B = function() {}
B.prototype = new A;
B.prototype.constructor = B;
What would take adverse effect if not?
EDIT
- As
@GGG&@CMShave explained, the constructor alignment takes no
effect on creating the child object bynew Child(...), but is
necessary to correctly reflect the child object’s constructor later. @GGGhas also suggested a defensive alternative to extend
the prototype chain. WhileChild.prototype = new Parent(...)
includes parent’s properties to child,Child.prototype =doesn’t.
Object.create(Parent.prototype)
First, please don’t do this:
Do this instead (shim
Object.createfor old browsers):As for
constructor, nothing will break if you don’t do this, but if you don’t:…setting the
constructorproperty of the prototype back to the actual constructor function allows you to do this: