Last active
December 23, 2015 00:59
-
-
Save 8bitDesigner/6557472 to your computer and use it in GitHub Desktop.
"Multiple inheritance" in Javascript
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
| function Parent() {} | |
| Parent.prototype.a = 'Foo' | |
| Parent.prototype.aGetter = function() { | |
| return this.a | |
| } | |
| function Child() { | |
| inherit(this, Parent) | |
| } | |
| var junior = new Child() | |
| var senior = new Parent() | |
| junior.aGetter() // retuns 'Foo' | |
| senior.a = 'Bar' | |
| senior.aGetter() // returns 'Bar' | |
| junior.aGetter() // retuns 'Foo' |
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
| function inherit(child, parent) { | |
| parent.call(child); | |
| Object.keys(parent.prototype).forEach(function(key) { | |
| var value = parent.prototype[key] | |
| if (typeof value === 'function') { | |
| child[key] = value.bind(child) | |
| } else { | |
| child[key] = value | |
| } | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment