Skip to content

Instantly share code, notes, and snippets.

@robdodson
Created August 9, 2012 18:01
Show Gist options
  • Select an option

  • Save robdodson/3306573 to your computer and use it in GitHub Desktop.

Select an option

Save robdodson/3306573 to your computer and use it in GitHub Desktop.
klass
(function() {
var klass = function(Parent, props) {
var Child, F, i;
// 1.
// new constructor
Child = function() {
// Call the super class constructor
if (Child._super && Child._super.hasOwnProperty('_construct')) {
Child._super._construct.apply(this, arguments);
}
// Call our constructor
if (Child.prototype.hasOwnProperty('_construct')) {
Child.prototype._construct.apply(this, arguments);
}
}
// 2.
// inherit
Parent = Parent || Object;
F = function() {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child._super = Parent.prototype;
Child.prototype.constructor = Child;
// 3.
// add implementation methods
for (i in props) {
if (props.hasOwnProperty(i)) {
Child.prototype[i] = props[i];
}
}
// 4.
// return the "class"
return Child;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment