Skip to content

Instantly share code, notes, and snippets.

@yckart
Last active December 15, 2015 13:29
Show Gist options
  • Select an option

  • Save yckart/5267295 to your computer and use it in GitHub Desktop.

Select an option

Save yckart/5267295 to your computer and use it in GitHub Desktop.
A class factory.
/**
* Class
* A class factory
*/
function Class(parent, members) {
// prepare single-inheritance
members = members || parent;
// setup proxy
var Proxy = function () {};
Proxy.prototype = (parent || Class).prototype;
// setup constructor
members.init = members.init || function () {};
var Shell = members.init;
// setup inheritance
Shell.prototype = new Proxy();
// setup identity
Shell.prototype.constructor = Shell;
// setup augmentation
Shell.extend = 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.extend(members);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment