Skip to content

Instantly share code, notes, and snippets.

@vpalos
Last active September 26, 2015 03:38
Show Gist options
  • Save vpalos/1033002 to your computer and use it in GitHub Desktop.
Save vpalos/1033002 to your computer and use it in GitHub Desktop.
JS: Simple Class implementation supporting deep inheritance and augmentation.
/**
* 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