Automatically bind methods to their class instance
It also correctly binds inherited properties.
Install
npm install auto-bind
Usage
import autoBind from 'auto-bind';class Unicorn { constructor(name) { this.name = name; autoBind(this); } message() { return `${this.name} is awesome!`; }}const unicorn = new Unicorn('Rainbow');// Grab the method off the class instanceconst message = unicorn.message;// Still bound to the class instancemessage();//=> 'Rainbow is awesome!'// Without `autoBind(this)`, the above would have resulted inmessage();//=> Error: Cannot read property 'name' of undefined