JSFiddle - React, Tailwind, and code Playground
JavaScript
// Define supercall
function A(x, y) {
if(y<=0){
throw new Error('some message');
}
this.x = x;
this.y = y;
console.log('A', x, y);
}
A.prototype.divide = function () {
console.log('divide', this.x/this.y);
};
// Define child classes constructor
function B(x, y) {
A.apply(this, arguments);
console.log('B', x, y);
}
// Create child's prototype – Without calling A
B.prototype = (function(parent, child){
function protoCreator(){
this.constructor = child.prototype.constructor
};
protoCreator.prototype = parent.prototype;
return new protoCreator();
})(A, B);
// Define child's methods
B.prototype.runB = function(){
console.log('runB', this.x);
}
// Use child
var b = new B(4, 6);
console.log(b.divide());
console.log(b.runB());
// Construction will fail since constructor of A fails
var b2 = new B(5, 0);