i'm kinda new to es6 features, i'm trying to implement some basic class, like this
class App {
constructor() {
this.init();
}
init() {
console.log("init!");
this.hello();
}
hello(){
console.log("Hello!");
}
}
let app = new App;
everything works fine. this is my output:
init
Hello!
then i tried to implement the same thing with publish / subscribe pattern (using PubSubJs plugin):
class App {
constructor() {
PubSub.subscribe('say hello', this.init);
}
init() {
console.log("init!");
this.hello();
}
hello(){
console.log("Hello!");
}
}
let app = new App;
PubSub.publish('say hello');
This will call correctly the init
method, but then will lose the class context when trying to call this.hello()
infact i get this output:
init //correct
Uncaught TypeError: this.hello is not a function //<- why this?
any idea? am i missing something? thanks