How I learned from a crazy idea / Reproduce the behavior in the 'old' way

by Arnaud Buchholz

HTML

<script src="https://arnaudbuchholz.github.io/blog/jsfiddle-assert.js"></script>

JavaScript

function OldA () {
  if (!(this instanceof OldA)) {
    throw new TypeError("Class constructor cannot be invoked without 'new'");
  }
  this._a = "A";
}

OldA.prototype.a = function () {
  return this._a;
};

var a = new OldA();
assert(() => a instanceof OldA);
assert(() => a.a() === "A");
assert(() => { try { OldA() } catch (e) { return e instanceof TypeError} );

function OldB () {
  OldA.apply(this, arguments);
  this._b = "B";
}

OldB.prototype = Object.create(OldA.prototype);

OldB.prototype.b = function () {
  return this._b;
};

var b = new OldB();
assert(() => b instanceof OldA);
assert(() => b.a() === "A");
assert(() => b instanceof OldB);
assert(() => b.b() === "B");