Created
July 17, 2013 04:25
-
-
Save leegrey/6017688 to your computer and use it in GitHub Desktop.
InheritMax.js is a more advanced version of Inherit.js. It produces classes that contain a self calling init() function. I now prefer the simpler, stripped back functionality of Inherit.js, but have posted this for reference.
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
/* | |
Author: Lee Grey, 2012 | |
License: MIT | |
USAGE: | |
var A = InheritMax.base( { | |
name: 'A', | |
say: function () { | |
console.log( 'A speaks' ); | |
} | |
} ); | |
var B = InheritMax.from( A, { | |
name: 'B', | |
init: function () { | |
console.log( 'B init()' ); | |
}, | |
say: function () { | |
console.log( 'B speaks' ); | |
} | |
}); | |
var C = InheritMax.from( B, { | |
name: 'C', | |
init: function () { | |
console.log( 'C init()' ); | |
}, | |
say: function () { | |
console.log( 'C speaks' ); | |
} | |
}); | |
//can just inherit from a plain object | |
var D = InheritMax.from( null, { | |
init: function () { | |
console.log( 'D init()' ); | |
}, | |
boo: function () { | |
console.log( 'D boo()' ); | |
} | |
} ); | |
*/ | |
// Util ////////////////////// | |
//ensure that Object.create works, using Crockford's shim: | |
if (typeof Object.create !== 'function') { | |
Object.create = function (o) { | |
function F() {} | |
F.prototype = o; | |
return new F(); | |
}; | |
} | |
// InheritMax //////////////////// | |
InheritMax = {}; | |
InheritMax.ClassBase = function () { | |
if( typeof this.init === 'function' ) { | |
this.init(); | |
} | |
} | |
// a convenience function for augmenting a class | |
InheritMax.ClassBase.augment = function ( props ) { | |
if( typeof props !== 'object' ) return; | |
for ( var name in props ) { | |
this.prototype[ name ] = props[ name ]; | |
} | |
//return this to enable chaining | |
return this; | |
}; | |
// returns a self-intialising base class | |
InheritMax.base = function ( props ) { | |
return InheritMax.from( null, props ); | |
} | |
InheritMax.from = function ( parent, props ) { | |
return InheritMax.overlay( parent, props ); | |
} | |
InheritMax.enhance = function () { | |
if( arguments.length < 2 ) return; | |
var target = arguments[ 0 ]; | |
var parent; | |
for ( i = 1; i < arguments.length; i++ ) { | |
parent = arguments[ i ]; | |
// If parent is a functional object, copy | |
// from parent.prototype into the target.prototype: | |
if( typeof parent === 'function' ) { | |
for ( var name in parent.prototype ) { | |
target.prototype[ name ] = parent.prototype[ name ]; | |
} | |
//copy props from parent into the target ( ie static vars and methods ) | |
for ( var name in parent ) { | |
target[ name ] = parent[ name ]; | |
} | |
} | |
//if it is an object, apply to the prototype | |
else if( typeof parent === 'object' ) { | |
for ( var name in parent ) { | |
target.prototype[ name ] = parent[ name ]; | |
} | |
} | |
} | |
} | |
InheritMax.overlay = function ( ) { | |
var i; | |
var parent; | |
var child = ( | |
function () { | |
return function () { | |
if( typeof this.init === 'function' ) { | |
this.init(); | |
} | |
} | |
} | |
)(); | |
for ( i = 0; i < arguments.length; i++ ) { | |
InheritMax.enhance( child, arguments[ i ] ); | |
} | |
return child; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment