You are reading a single comment by @Raik and its replies. Click here to read the full conversation.
  • This is about object reference.

    change this. to SOUND. in line 21.

    setInterval(function(){SOUND.beep(freq,t­ime); }, 1000);
    

    or add ‘that’

    
    function SOUND(pin,volume){
        this.PIN = pin;
        this.VOLUME = volume;
        var that = this;
    }
    
    SOUND.prototype.multibeep_works_not = function(freq,time,nr){
      setInterval(function(){ that.beep(freq,time); }, 1000);
    };
    
  • @Raik, is the above code in one monolithic file, as the modules file, or is there a calling page?

    It's the piece of code from the editor I upload to experiment with modules. Later on this will be put to the file system as one file (that's the plan).

    It's possible that L21 needs to also pass 'freq' 'time' as arguments

    Well it's referring to the beep function, that only has two argument. So it should work, right?

    change this. to SOUND. in line 21.
    or add ‘that’

    Thanks for the hint: creating a new reference for this worked, though I'm not sure why?

    Is it because the module code runs in its own scope? And this is not referring to SOUND but rather to the main scope?

    When a module is loaded, Espruino executes the file in its own scope
    (from Writing Modules)

  • It's "just classic javascript this" :) It's not a because "the module code runs in its own scope".
    At line 21, the this inside the callback function inside the setInterval is not what you would expect. Basically nothing / global scope, because you did not specify the this argument, or used bind. Just JS default behavior. :)
    That's why code was littered with var that = this. But now it's kind-of sort-of solved in ES6 / typescript code.

About

Avatar for Raik @Raik started