That's strange... bind should really fix the can't create on undefined error.
What happens is this.serial.on calls your function, but it calls it as a function, rather than as a method call... so this never gets set to the correct thing.
this.serial.on( 'data', this.newData.bind(this) ); is a bit like saying:
x = this;
this.serial.on( 'data', function(a) {
x.newData(a);
});
So it makes sure that newData gets called on the correct object. Hope that makes sense!
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.
That's strange...
bind
should really fix thecan't create on undefined
error.What happens is
this.serial.on
calls your function, but it calls it as a function, rather than as a method call... sothis
never gets set to the correct thing.this.serial.on( 'data', this.newData.bind(this) );
is a bit like saying:So it makes sure that
newData
gets called on the correct object. Hope that makes sense!