Created
March 31, 2012 06:51
-
-
Save creynders/2260118 to your computer and use it in GitHub Desktop.
JavaScript generic inheritance methods
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
var Obj = { | |
createProxy : function( o ) { | |
var F = function () { | |
}; | |
F.prototype = o; | |
return F; | |
}, | |
createObject : function( o ){ | |
//Crockford Object.create | |
var F = Obj.createProxy( o ); | |
return new F(); | |
}, | |
extend : function( TargetClass, SourceClass ) { | |
TargetClass.prototype = Obj.createObject( SourceClass.prototype ); | |
TargetClass.__super__ = SourceClass; | |
TargetClass.prototype.constructor = TargetClass; | |
}, | |
copyMembers : function( target, source ) { | |
var p = source.prototype; | |
for ( var member in p ) { | |
if ( p.hasOwnProperty( member ) ) { | |
target.prototype[ member ] = p[ member ]; | |
} | |
}; | |
return target; | |
}, | |
decorate : function( target, SourceClass ){ | |
var F = Obj.createProxy( target ); | |
F = Obj.copyMembers( F, SourceClass ); | |
var instance = new F(); | |
SourceClass.call( instance ); | |
return instance; | |
} | |
}; |
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
var Base = function(){ | |
console.log( '-- Base --' ); | |
this.baseArr = []; | |
this.something = 'something'; | |
this.init(); | |
}; | |
Base.prototype = { | |
init : function(){ | |
console.log( 'Base init' ); | |
}, | |
logSomething : function(){ | |
console.log( this.something ); | |
return this.something; | |
} | |
}; | |
var Foo = function(){ | |
Foo.__super__.call( this ); //call super constructor | |
console.log( '-- Foo --' ); | |
this.bar = 'bar'; | |
this.fooArr = []; | |
this.logBar0 = function(){ | |
console.log( '#0 ' + this.bar ); | |
return this.bar; | |
} | |
}; | |
Obj.extend( Foo, Base ); //classical inheritance | |
Foo.prototype.logBar2 = function(){ | |
console.log( '#2 ' + this.bar ); | |
return this.bar; | |
}; | |
var Baz = function(){ | |
console.log( '-- Baz --' ); | |
this.qux = 'qux'; | |
this.bazArr = []; | |
}; | |
Baz.prototype = { | |
logQux : function(){ | |
console.log( this.qux ); | |
return this.qux | |
} | |
} |
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
//classical inheritance | |
var foo = new Foo(); //Foo already extends Base, see classes.js | |
foo.logSomething(); | |
foo.logBar0(); | |
foo.logBar2(); | |
//object inheritance from "class" | |
var baz = new Baz(); | |
baz = Obj.decorate( baz, Base ); //only the instance baz is decorated, the prototype of Base is unaltered | |
baz.logQux(); | |
baz.logSomething(); | |
//object inheritance | |
var mofo = Obj.createObject( baz ); | |
mofo.logSomething(); | |
mofo.logQux(); |
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
Copyright (c) 2012, Camille Reynders | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment