You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • 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:

    function EasyVR(ser,onCm,onTo) {
      this.ser = ser;
      this.onCommand=onCm;
      this.onTimeout=onTo;
      var evr = this;             // <------------------------------
      this.ser.on('data',function(){evr.onWatc­h(); }); // <--------------
      this.vrstate=-1;
      this.stsr='o';
      this.rcvv="";
    } ;
    EasyVR.prototype.onWatch=function(data) {
        console.log(this);
    };
    

    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 :(

About

Avatar for Gordon @Gordon started