Skip to content

Instantly share code, notes, and snippets.

@lackneets
Created January 26, 2016 09:42
Show Gist options
  • Save lackneets/820cf6421d427bf3c137 to your computer and use it in GitHub Desktop.
Save lackneets/820cf6421d427bf3c137 to your computer and use it in GitHub Desktop.
ECMA5 Class inheritance inspired by Babel
"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