Skip to content

Instantly share code, notes, and snippets.

@Jon889
Last active December 29, 2015 09:49
Show Gist options
  • Save Jon889/7653274 to your computer and use it in GitHub Desktop.
Save Jon889/7653274 to your computer and use it in GitHub Desktop.
Javascript Classes More succinct (read hacky) way of making "Classes" in javascript.
//More succinct (read hacky) way of making "Classes" in javascript.
//Use:
/*
Class({NewClass : Superclass}, [
function methodOne() {
//do something
}
});
then use in the usual javascript way:
var x = NewClass();
x.methodOne();
*/
function Class(obj, arr) {
function inherit(name, v) {
window[name] = new Function("return function " + name + "() {}")();
this.prototype = new v();
this.prototype.constructor = window[name];
}
for (key in obj) {
if (obj.hasOwnProperty(key)) {
inherit(key, obj[key]);
for (fn in arr) {
window[key].prototype[arr[fn].name] = arr[fn];
}
return window[key];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment