Skip to content

Instantly share code, notes, and snippets.

@JustinTW
Created January 25, 2016 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinTW/2d7e4b5af2de5cfaaba2 to your computer and use it in GitHub Desktop.
Save JustinTW/2d7e4b5af2de5cfaaba2 to your computer and use it in GitHub Desktop.
klass.js
var klass = function (Parent, props) {
var Child, F, i;
// 1.
// new constructor
Child = function () {
if (Child.uber && Child.uber.hasOwnProperty("__construct")) {
Child.uber.__construct.apply(this, arguments);
}
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.uber = Parent.prototype;
Child.prototype.constructor = Child;
// 3.
// add implementation methods
for (i in props) {
if (props.hasOwnProperty(i)) {
Child.prototype[i] = props[i];
}
}
// return the "class"
return Child;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment