Last active
December 18, 2015 07:09
-
-
Save tlivings/5744253 to your computer and use it in GitHub Desktop.
Javascript extend utility function.
This file contains hidden or 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
| /** | |
| * Extend an Object from another Object. | |
| * @param child | |
| * @param parent | |
| * @param proto - prototype mixins | |
| * @returns {*} | |
| */ | |
| function(child, parent, proto) { | |
| //Constructor | |
| var properties = { | |
| constructor: { | |
| value: child | |
| } | |
| }; | |
| //Mixin prototype properties | |
| if(proto) { | |
| Object.keys(proto).forEach(function(key) { | |
| properties[key] = { | |
| value : proto[key] | |
| } | |
| }); | |
| } | |
| //Extend | |
| child.prototype = Object.create(parent.prototype, properties); | |
| //Convenience object | |
| child.super_ = parent; | |
| return child; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment