Possibly this should actually be set to the serial object the event was on though - I'd need to figure out what NodeJS does. Even if that was the case, just calling this.ser.on('data',this.onWatch); wouldn't do what you'd want.
There's some stuff on StackOverflow about it, and a neat solution would be to use bind - although I'm afraid that's not implemented yet :(
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I think this is actually exactly the same issue as you'd have with
setTimeout
-this
isn't set to the right thing.To solve it you'd define a variable in your function's scope and then use that:
Possibly
this
should actually be set to the serial object the event was on though - I'd need to figure out what NodeJS does. Even if that was the case, just callingthis.ser.on('data',this.onWatch);
wouldn't do what you'd want.There's some stuff on StackOverflow about it, and a neat solution would be to use
bind
- although I'm afraid that's not implemented yet :(