Created
November 19, 2015 20:51
-
-
Save jacwright/e9530821aa8c3e237646 to your computer and use it in GitHub Desktop.
Fix class.extend to support class.static and removes mixins
This file contains 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 Class() {} | |
Class.extend = extend; | |
module.exports = Class; | |
function extend(Subclass, prototype) { | |
// Support no constructor | |
if (typeof Subclass !== 'function') { | |
prototype = Subclass; | |
var SuperClass = this; | |
Subclass = function() { | |
SuperClass.apply(this, arguments); | |
}; | |
} | |
Subclass.extend = extend; | |
extendStatics(this, Subclass, prototype); | |
var descriptors = getDescriptors(prototype); | |
descriptors.constructor = { writable: true, configurable: true, value: Subclass }; | |
Subclass.prototype = Object.create(this.prototype, descriptors); | |
return Subclass; | |
} | |
// Get descriptors (allows for getters and setters) and sets functions to be non-enumerable | |
function getDescriptors(object) { | |
var descriptors = {}; | |
if (object) { | |
Object.getOwnPropertyNames(object).forEach(function(name) { | |
descriptors[name] = Object.getOwnPropertyDescriptor(object, name); | |
}); | |
} | |
return descriptors; | |
} | |
// Copies static methods over for static inheritance | |
function extendStatics(Class, Subclass, prototype) { | |
var descriptors; | |
if (prototype && prototype.static) { | |
descriptors = getDescriptors(prototype.static); | |
delete prototype.static; | |
} | |
Subclass.static = Object.create(Class.static || null, descriptors); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment