0

I'm new to ES6 and try to migrate old javascript code. Now my class use methods but is not clear why not all methods are attachetd to 'this'. Is not even clear if i can use methods as function (and pass as callback)

class MyClass{
    construtor (parameters)
    {
        this.myInternalState = new ExternalClass ( this.method1CallBack );
        this.varClass1 
        this.varClass2...
    }

    method1CallBack (){} //OK attached to this
    method2 (parameter){} //KO not found in this object
}

Is not clear where is my mistake, in code syntax or in the interpretation of the method's use.

Skary
  • 1,322
  • 1
  • 13
  • 40
  • 1
    `KO not found in this object` what? 3 – Olian04 May 19 '19 at 20:23
  • debbuging the code i was not able to find 'method2' in the 'this' inside 'method1CallBack '. But more i debug and more i suspect that the problem is in the caller. Are there any issue with the scope if the caller is not ES6? – Skary May 19 '19 at 20:38
  • 1
    You have to bind the callback to `this`. Like so: `new ExternalClass(this.method1CallBack.bind(this))` – Olian04 May 19 '19 at 20:47
  • @Skary This has nothing to do with ES6. `this` works just like it always did for methods. – Bergi May 19 '19 at 21:38

0 Answers0