Last active
September 26, 2015 03:38
-
-
Save vpalos/1033002 to your computer and use it in GitHub Desktop.
JS: Simple Class implementation supporting deep inheritance and augmentation.
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
/** | |
* Class.js: A class factory. | |
* (http://vpalos.com/1194/js-classes-for-the-masses/) | |
*/ | |
function Class(members) { | |
// setup proxy | |
var Proxy = function() {}; | |
Proxy.prototype = (members.base || Class).prototype; | |
// setup constructor | |
members.init = members.init || function() { | |
if (Proxy.prototype.hasOwnProperty("init")) { | |
Proxy.prototype.init.apply(this, arguments); | |
} | |
}; | |
var Shell = members.init; | |
// setup inheritance | |
Shell.prototype = new Proxy(); | |
Shell.prototype.base = Proxy.prototype; | |
// setup identity | |
Shell.prototype.constructor = Shell; | |
// setup augmentation | |
Shell.grow = function(items) { | |
for (var item in items) { | |
if (!Shell.prototype.hasOwnProperty(item)) { | |
Shell.prototype[item] = items[item]; | |
} | |
} | |
return Shell; | |
}; | |
// attach members and return the new class | |
return Shell.grow(members); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment