Last active
December 13, 2015 22:29
-
-
Save lennerd/4984473 to your computer and use it in GitHub Desktop.
A way to have classes in Javascript
This file contains hidden or 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
// Orginial version from John Resig (http://ejohn.org/blog/simple-javascript-inheritance/) | |
(function(){ | |
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b\__super\b/ : /.*/; | |
this.Class = function(){}; | |
this.Class.create = function(prop) { | |
var super = this.prototype, name, prototype; | |
initializing = true; | |
prototype = new this(); | |
initializing = false; | |
for (name in prop) { | |
prototype[name] = typeof prop[name] == "function" && | |
typeof super[name] == "function" && fnTest.test(prop[name]) ? | |
(function(name, fn){ | |
return function() { | |
var tmp, ret; | |
try { | |
tmp = this.__super; | |
this.__super = super[name]; | |
ret = fn.apply(this, arguments); | |
} finally { | |
this.__super = tmp; | |
} | |
return ret; | |
}; | |
})(name, prop[name]) : | |
prop[name]; | |
} | |
function Class() { | |
if (!initializing && this.__init) | |
this.__init.apply(this, arguments); | |
} | |
Class.prototype = prototype; | |
Class.prototype.constructor = Class; | |
Class.extend = arguments.callee; | |
return Class; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment