Skip to content

Instantly share code, notes, and snippets.

@fancyboynet
Created August 22, 2018 07:12
Show Gist options
  • Save fancyboynet/d5785f87cf385035c634027c20d074f9 to your computer and use it in GitHub Desktop.
Save fancyboynet/d5785f87cf385035c634027c20d074f9 to your computer and use it in GitHub Desktop.
Automatically bind methods to their class instance
class AutoBindMethod {
constructor () {
Object.getOwnPropertyNames(this.constructor.prototype).forEach(m => {
this[m] = this[m].bind(this)
})
}
}
class Demo extends AutoBindMethod {
constructor () {
super()
document.addEventListener('click', this.hello)
}
hello () {
console.log(this)
}
}
new Demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment