The following code generates the error TypeError: this.SetValue is not a function
.
class FlightData {
constructor(app)
{
this.app = app;
this.websocket = new WebSocket('ws://'+window.location.hostname+':20225/flight_data');
this.websocket.addEventListener('message', this.HandleSocketMessage);
}
SetValue(name, value)
{
console.log(name + ": " + value);
}
HandleSocketMessage(e)
{
this.SetValue("name", "value");
}
}
If I do the following instead, it works. However I am trying to avoid closures (even though the one here is harmless) in my code:
this.websocket.addEventListener('message', e => {
this.SetValue("name", "value");
});
Can someone please explain why the code at the top doesn't work and what I need to do to make it work?