Skip to content

Instantly share code, notes, and snippets.

@ryardley
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryardley/f9c6ce80ea1c72318363 to your computer and use it in GitHub Desktop.
Save ryardley/f9c6ce80ea1c72318363 to your computer and use it in GitHub Desktop.
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