Created
January 26, 2016 09:42
-
-
Save lackneets/820cf6421d427bf3c137 to your computer and use it in GitHub Desktop.
ECMA5 Class inheritance inspired by Babel
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"; | |
function classDefine(subClass, prototype, staticMembers){ | |
return classInherit(Object, subClass, prototype, staticMembers); | |
} | |
function classInherit(superClass, subClass, prototype, staticMembers){ | |
subClass.prototype = Object.create(superClass.prototype, { | |
super: {value: superClass, enumerable: false, writable: false, configurable: true }, | |
constructor: {value: subClass, enumerable: false, writable: false, configurable: true }, | |
}); // Extend the prototype from super class | |
subClass.__proto__ = superClass; // Link the actual proto chaining | |
Object.assign(subClass.prototype, prototype || {}); // Shortcut to define sub class instance methods | |
Object.assign(subClass, staticMembers || {}); // Shortcut to define sub class instance methods | |
return subClass; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment