-
-
Save ryardley/f9c6ce80ea1c72318363 to your computer and use it in GitHub Desktop.
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
function createBase(){ | |
// This function recursively and distructively | |
// merges the guest object onto the host object | |
function merge(host, guest) { | |
// cycle through guest's properties | |
for(var prop in guest) { | |
if(guest.hasOwnProperty(prop)){ | |
// if the property is an object then run | |
// the merge on it or just copy it over | |
host[prop] = (typeof guest[prop] === 'object') ? | |
merge(host[prop], guest[prop]) : | |
guest[prop]; | |
} | |
} | |
return host; | |
} | |
// This function processes the declaritave function | |
// as well as manages the extension | |
function extend(factoryFunction){ | |
// run the factoryFunction to get the declaration object | |
var properties = factoryFunction(), | |
// extract the constructor function reference | |
BaseClass = properties.init, | |
// extract the super class reference | |
SuperClass = this; | |
// copy extend function across | |
BaseClass.extend = SuperClass.extend; | |
// create the inheritance heirarchy | |
BaseClass.prototype = Object.create(SuperClass.prototype); | |
// merge the new properties with those given from inheritance | |
merge(BaseClass.prototype, properties); | |
// return the class ready for use. | |
return BaseClass; | |
} | |
function Base(){} | |
Base.extend = extend; | |
return Base; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment