You are reading a single comment by @MaBe 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);
    };
    
  • Sun 2020.01.26

    @MaBe, (haven't tested) but wouldn't var 'that' L5 need to be global in order for L9 to see object 'that'? . . . or maybe (guessing) SOUND.that.beep(?

    My belief was the (missing) argument syntax as shown in that link.

  • @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)

About

Avatar for MaBe @MaBe started