Last active
November 24, 2016 14:49
-
-
Save vvakame/adf62994b9c4da0a27573b075295e07d to your computer and use it in GitHub Desktop.
ECMAScript 2016 compatible class inheritance
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
"use strict"; | |
{ // sample | |
class Base { | |
static hello() { | |
return "test"; | |
} | |
} | |
class Inherit extends Base { | |
} | |
console.log(Inherit.hello()); | |
} | |
function Base () { | |
} | |
Object.defineProperty(Base, "hello", { | |
value: function() { | |
return "test"; | |
}, | |
enumerable: false, | |
}); | |
function Inherit() { | |
} | |
function inheritOfClassDefinitionEvaluation(D, B) { | |
let protoParent = B.prototype; | |
if(typeof protoParent !== "object" && typeof protoParent !== "function") { | |
throw new TypeError(); | |
} | |
let constructorParent = B; | |
let proto = Object.create(protoParent); | |
let constructor = D; | |
// DefineMethod for constructor with arguments proto and constructorParent as the optional functionPrototype argument. | |
if (typeof Object.setPrototypeOf === "function") { | |
Object.setPrototypeOf(constructor, constructorParent); | |
} else { | |
constructor.__proto__ = constructorParent; | |
} | |
let F = constructor; | |
Object.defineProperty(F, "prototype", { | |
value: proto, | |
writable: false, | |
enumerable: false, | |
configurable: false, | |
}) | |
Object.defineProperty(proto, "constructor", { | |
value: F, | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
}); | |
return F; | |
} | |
inheritOfClassDefinitionEvaluation(Inherit, Base); | |
console.log(Inherit.hello()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment