javascript - ES6 call one method from another -
this question has answer here:
i new es6 syntax, original code has more implementation, have simplified. getting error saying cannot read property 'method2' of undefined. doing wrong here ? have bind calls.
class class1 { constructor() { eventbus.subscribe(this.method1); } method1() { this.method2(); } method2(){ } }
you need eventbus.subscribe(this.method1.bind(this));
since associating method run on other place, sure under scope run. forcing bind assure instance of class1
used.
edit: since es6 allow arrow functions can eventbus.subscribe(() => this.method1());
, refereed @torazaburo
Comments
Post a Comment