Function declarations not hoisted?

Posted on
  • This:

    function onInit () {
      setDeepSleep(1);
      setBusyIndicator(LED1);
      setSleepIndicator(LED3);
    
      var t;
      setInterval(function () {
        t= E.getTemperature();
      }, 3000);
      
      setWatch(flip, BTN, {repeat:true, edge:'both'});
      
      function flip (a) {
        digitalWrite(LED2, a.state);
      }
    }
    
    Running onInit()...
    Uncaught ReferenceError: "flip" is not defined
     at line 10 col 12
      setWatch(flip, BTN, {repeat:true, edge:'both'});
               ^
    in function called from system
    

    ??

  • Yes, unfortunately Espruino doesn't do Function Hoisting.

    Due to the way Espruino executes code it parses as
    it executes. Function Hoisting wasn't implemented as it would require
    two passes, which would drastically slow down execution.

  • Oh, ok, thanks! Is that said somewhere in the docs?

  • One more question please :-) I don't understand why does the red led stay on with the script above, until I press the BTN or the first timeout times out?

  • Actually it looks like it wasn't (I think it's been mentioned in github/forum a bit) but as we now have http://www.espruino.com/Features I've added a specific mention of it (it'll go live in a few days).

  • Thanks, again, Mr. Williams!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Function declarations not hoisted?

Posted by Avatar for Georg @Georg

Actions