Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
Created October 6, 2010 03:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelficarra/612786 to your computer and use it in GitHub Desktop.
Save michaelficarra/612786 to your computer and use it in GitHub Desktop.
JS classical OOP
__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