Last active
March 7, 2024 01:06
-
-
Save Stuk/3f60990c3e64ed49848419dfa9454f08 to your computer and use it in GitHub Desktop.
ES5 classes, compatible with ES6 classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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