Skip to content

Instantly share code, notes, and snippets.

@Stuk
Last active March 7, 2024 01:06
Show Gist options
  • Save Stuk/3f60990c3e64ed49848419dfa9454f08 to your computer and use it in GitHub Desktop.
Save Stuk/3f60990c3e64ed49848419dfa9454f08 to your computer and use it in GitHub Desktop.
ES5 classes, compatible with ES6 classes
function A() {}
A.prototype.thing = function () {
return "A";
};
function B() {}
Object.setPrototypeOf(B, A)
B.prototype = Object.create(A.prototype)
B.prototype.constructor = B;
B.prototype.thing = function () {
return A.prototype.thing.call(this) + "B";
};
// ---
function assert(ok) { if (!ok) throw new Error("Assertion failed") };
var a = new A();
var b = new B();
assert(a.constructor === A);
assert(b.constructor === B);
assert(a.thing() === "A");
assert(b.thing() === "AB");
assert(b instanceof A);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment