Skip to content

Instantly share code, notes, and snippets.

@spjwebster
Created February 16, 2014 08:33
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 spjwebster/9031186 to your computer and use it in GitHub Desktop.
Save spjwebster/9031186 to your computer and use it in GitHub Desktop.
function augment(parent, properties) {
var child = properties.constructor || function() {
return parent.apply(this, arguments);
};
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
for (var key in properties) {
child.prototype[key] = properties[key];
}
return child;
};
// ... the nasty bit of JavaScript-specific business there being the intermediate Surrogate, so that you don't need a concrete instance of the parent class to be instantiated, in order to set your prototype chain. Used like so:
var Person = augment(Model, {
sortableName: function() {
return this.lastName + ', ' + this.firstName;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment