6

I am new to ES6 syntax, my original code has more implementation, but I have simplified. I am getting an error saying cannot read property 'Method2' of undefined. What am I doing wrong here ? Do I have to do any bind calls.

class Class1 {
  constructor() {      
     eventbus.subscribe(this.Method1);  
    }

  Method1() {
   this.Method2(); 
  }

  Method2(){    
  }  
}
anivas
  • 6,437
  • 6
  • 37
  • 45
  • 1
    Please show all of the relevant code. In JavaScript, most important things happen at *call time*, so show the actual call to `.Method1()`, and some code surrounding it. – Anders Marzi Tornblad Jan 27 '16 at 13:36
  • Tell us too how are you running that. With Babel for instance? – anolsi Jan 27 '16 at 13:37
  • How are you calling `Method1`? JavaScript's invocation contexts are pretty weird: http://codepen.io/vinhnghi223/pen/ghpvt – James Wright Jan 27 '16 at 13:37
  • The code you've posted [works fine if I imagine what the surrounding/calling code looks like](https://jsfiddle.net/q2s52672/). – Jeroen Jan 27 '16 at 13:38
  • How do you create your object? Your error message say that your object is not instantiated (anymore). – Adrian Jan 27 '16 at 13:39
  • `new Class1().Method1();` are you doing something like this? – Jai Jan 27 '16 at 13:42
  • What's a "private method"? – Bergi Jan 27 '16 at 14:29
  • This question has been asked a number of times. I'm too lazy right now to find the dup. The classic case is passing a "class' method to `setTimeout`. –  Jan 27 '16 at 15:25
  • With all due respect, there's no need to see any more code. *How are you calling `Method1`? Obviously, via the `eventbus.subscribe` call. –  Jan 27 '16 at 15:27
  • I will not close this as dup but this is very close: http://stackoverflow.com/questions/7890685/referencing-this-inside-setinterval-settimeout-within-object-prototype-methods –  Jan 27 '16 at 15:44
  • See "Common problem: Using object methods as callbacks / event handlers" in the duplicate. – Felix Kling Jan 27 '16 at 22:14

2 Answers2

2

cannot read property 'Method2' of undefined

Most probably you are not creating any object of same class or may be you are missing the new keyword to create new object.

var cls = Class1(); // will throw error.

cls.Method1(); 

What i meant if you do:

var cls = new Class1();

cls.Method1(); // will get executed.

es6fiddle demo.

Jai
  • 74,255
  • 12
  • 74
  • 103
2

You need to do eventbus.subscribe(this.Method1.bind(this));

Since you are associating the method to run on other place, you will be sure under which scope it will run. Forcing the bind to this will assure that is the instance of Class1 that will be used.

EDIT: Since ES6 allow arrow functions you can also do eventbus.subscribe(() => this.Method1());, as it was refereed by @torazaburo

anolsi
  • 529
  • 1
  • 11
  • 21
  • 2
    Personally I would prefer `eventbus.subscribe(() => this.Method1())` now that we have arrow functions. –  Jan 27 '16 at 15:46
  • Yes, you are correct. It will also work. I have edited my answser – anolsi Jan 27 '16 at 16:43