• Assumed in the top / global level:

    A) What is the difference between line 1 and 2 below?

    function b() { console.log("b()"); }
    var b = function() { console.log("b()"); };
    

    B) With A) answered, what is the difference between the two blocks below?

    function a() {
      function b() { console.log("b()"); }
      setTimeout(b,500);
    }
    
    function a() {
      var b = function(){ console.log("b()"); }
      setTimeout(b,500);
    }
    

    C):

    if you called it twice it'd be able to use the same copy of b's code, saving memory.

    I assume you mean: called it twice within funcation a(), correct?

    I expect that assigning a function to a variable var b = function(){} is just putting a pointer - like the entry point of the function (the source address) - into the (scoped) variable - same as function b(){} , and then 'skips' the whole function in regard of execution 'to just find the end of the function' in order to resume execution.

    function b parsed beforehand, so execution would be faster.

    ...is that not a contradiction to Espruino executes from source code?

    Because what is created on parsing? If it is more than some 'isParsed' flag that is stored within the 'variable/function' value reference store, then it goes towards JIT compiling and uses unpredictable amounts of memory. And if it is just a flag, what would be the use of it? ...so far I did not find the time - or guts(?) - yet to dive into the interpreter's gut (source code).

About

Avatar for allObjects @allObjects started