There are 2 ways to call the parent constructor in the child.
var A = function A() {
this.x = 123;
};
var B = function B() {
// 1. call directly
A.call(this);
// 2. call from prototype
A.prototype.constructor.call(this);
};
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
Are there any situations when one would be safer/better than the other, or are they always equivalent?
It’s always better to use the base constructor directly for the following reasons:
prototype.constructor.Ainherits fromC, but I forgot to setA.prototype.constructorback toA. So it now points toC. This causes problems in the constructorBif we use the second method: