Create a gist now

Instantly share code, notes, and snippets.

@ryardley /base_object.js Secret
Last active Aug 29, 2015

What would you like to do?
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