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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -