Skip to content

Instantly share code, notes, and snippets.

@vvakame
Last active November 24, 2016 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvakame/adf62994b9c4da0a27573b075295e07d to your computer and use it in GitHub Desktop.
Save vvakame/adf62994b9c4da0a27573b075295e07d to your computer and use it in GitHub Desktop.
ECMAScript 2016 compatible class inheritance
"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