Created
February 16, 2014 08:33
-
-
Save spjwebster/9031186 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 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