You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • DrAzzy, sorry to respond late on your first post... was out all day on water whale watching...

    Your initial approach is just fine... the problem with JS is that this is super-late-binding: when it comes to execution, this is the context at execution time... with a simple cheat you can get it running without having to violate encapsulation nor venture into globals (...as you got it working - no offense what so ever to be taken here of).

    function EasyVR(ser,onCm,onTo) {
      var _this = this;
      this.ser = ser;
      this.onCommand=onCm;
      this.onTimeout=onTo;
      this.ser.on('data',_this.onWatch);
      this.vrstate=-1;
      this.stsr='o';
      this.rcvv="";
    } ;
    

    As you notice, you make a copy _this of the correct this reference to this in line 2 and use it in line 6. This resolves the issue of the runtime event, where serial receives data and has to invoke a the particular - in creation - object instance's method .onWatch() is called.

    This is sufficient if you do not need to call this in .onWatch(), because only the proper (prototype) method is passed as callback for the serial.on().

    In case you need to reference this in .onWatch() - as you do with the simple example you use with console.log(this) - you need to do more, because with passing the right method you did not pass the right context yet... after all, JavaScript IS a functional language - call subroutine - and not really an object language with comprehensive encapsulation of behavior AND state - send message with parm obj refs to object (also referenced to as invocation of a method of an object with parm obj refs).

    Nice 'concidence' to me after just looking up Lambda_calculus on, section of Lambda calculus and programming languages - Anonymous functions: it describes almost to a tee the language teachings and sequence and thinking about algorithms and data structures I was exposed to : Pascal (S) - 'brain child' of ALGOL 60 - Smalltalk - Javascript. In same section under 'more recent' C++11 is mentioned. Java is still missing... (explains 'needs Edit' comment). All other languages I got used to use were like noise on the road side.

    function EasyVR(ser,onCm,onTo) {
      var _this = this;
      this.ser = ser;
      this.onCommand=onCm;
      this.onTimeout=onTo;
      this.ser.on('data',function(data){ _this.onWatch(data); });
      this.vrstate=-1;
      this.stsr='o';
      this.rcvv="";
    } ;
    

    This will fix this. ...nice word play (at least in the eyes - and ears - of a non native English speaker...).

    On the Web you find that for _this - but I like to stick with the name as close to what it means... and declared it to myself almost as a reserved word, like this.

    What frameworks - as mentioned in many forum entries mention - are doing with the bind is essentially what is done in line 6: make sure the function is called in the context of the object, so it is more a method invocation of an object then just a function call. It is binding the function to the object context. Unfortunately, most of all the good things that come with the frameworks are just too much bulky for resource frugal Espruino... just like V8 was to be useful. And even more unfortunate, that more an more Web entries under Javascript give solutions for problems not in Javascript but in frameworks, and Javascript core knowledge shrinks to being lost.

    Java and 'real object oriented language' proponents will call this still hideous. But liking almost all languages, I appreciate the freedom I get with Javascript for a very small price... over (m)any other languages. Javascript got the Lambda calculus right in its initial release - when still called Lifescript and not even called Javascript yet, where it took Java 8 major releases to get there. Agreed: you could do Javascript-similar things with Java too - much more safely - but also much much much more hideously... Way more 'hideousness' than what is needed to make Javascript to have what Java has in regard of inheritance and polymorphism / class hierarchy).

About

Avatar for allObjects @allObjects started