Created
October 6, 2010 03:53
-
-
Save michaelficarra/612786 to your computer and use it in GitHub Desktop.
JS classical OOP
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
__extends = function(parent, child) { | |
var ctor = function(){}; | |
ctor.prototype = parent.prototype; | |
child.prototype = new ctor; | |
child.prototype.constructor = child; | |
if(typeof parent.extended === 'function') parent.extended(child); | |
child.__super__ = parent.prototype; | |
return child; | |
}; | |
var Base = function Base(){}; | |
var C = (function(__super){ | |
function C(){ /* constructor */ } | |
function(){ | |
// class methods/properties | |
this.constructor.staticMethod = function(){}; | |
C.anotherStaticMethod = function(){}; | |
// local methods/variables | |
var local = function(){}; | |
// instance methods/properties | |
this.method = function(){};; | |
}.call(__extends(__super, C).prototype); | |
return C; | |
}(Base)); | |
// allow Ruby-style ClassName.new instantiation | |
Function.prototype['new'] = (function(){ | |
var ctor = function(){}; | |
return function(){ | |
ctor.prototype = this.prototype; | |
var child = new ctor; | |
var result = this.apply(child, arguments); | |
return result === Object(result) ? result : child; | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment