Created
August 22, 2018 07:12
-
-
Save fancyboynet/d5785f87cf385035c634027c20d074f9 to your computer and use it in GitHub Desktop.
Automatically bind methods to their class instance
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
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