I seem to be having scoping issues on a typescript class in which I'm trying to see if I can get rid of anonymous functions. I've been going between using the arrow method and not. So for example -
//blah is globally available
export class Foo {
addSomeListeners = () => {
blah.addListener( this.eventHandler );
}
eventHandler( someProperties ){
blah.get( somePropreties, this.anotherCallback );
}
anotherCallback = ( data ) => {
//do something with the data
}
}
So elsewhere in the code I create an instance of Foo and I call the addSomeListeners method. This works fine. Later an event is fired and the eventHandler method gets called - all still good.
Now inside the eventHandler method it does some stuff and calls a get method that needs a callback. Its at this point that the 'this' property is null - so it has no idea what 'anotherCallback' is.
I'm guessing its a simple scoping issues, but I can seem to figure it right now. I'd like to be able to remove all anonymous functions, so rather than define the method in the blah.get( ...) signature I can call a method from within the Foo class to reuse.
EDIT - This question is not a duplicate as the duplicate doesn't deal with Typescript, it does mention ECMA6 arrow functions but doesn't solve the issue I am having. So yes very similar, but not similar enough IMO to be a duplicate.
So for example I have an instance of Foo (say foo) where I call foo.addSomeListeners(). Now in that method 'this' is foo - which is what I'd expect. Then a bit later the eventHandler method gets called due to the event - but inside that 'this' is null.