Skip to content

Instantly share code, notes, and snippets.

@ianb
Last active December 11, 2015 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianb/4565787 to your computer and use it in GitHub Desktop.
Save ianb/4565787 to your computer and use it in GitHub Desktop.
A simple implementation of a class pattern for Javascript
var Class = function (superClass, prototype) {
if (prototype === undefined) {
prototype = superClass;
} else {
var newPrototype = Object.create(superClass);
for (var a in prototype) {
newPrototype[a] = prototype[a];
}
prototype = newPrototype;
}
var ClassObject = function () {
var obj = Object.create(prototype);
obj.constructor.apply(obj, arguments);
return obj;
};
ClassObject.prototype = prototype;
return ClassObject;
};
// Usage:
var Person = Class({
constructor: function (name) {
this.name = name;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment