Skip to content

Instantly share code, notes, and snippets.

@8bitDesigner
Last active December 23, 2015 00:59
Show Gist options
  • Select an option

  • Save 8bitDesigner/6557472 to your computer and use it in GitHub Desktop.

Select an option

Save 8bitDesigner/6557472 to your computer and use it in GitHub Desktop.
"Multiple inheritance" in Javascript
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'
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