Last active
December 29, 2015 09:49
-
-
Save Jon889/7653274 to your computer and use it in GitHub Desktop.
Javascript Classes
More succinct (read hacky) way of making "Classes" in javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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