Scope of eval

Posted on
  • I have tried to eval some code inside a function and it seems to be running in the global scope and not that of the function.

    var global = "a";
    function test(){
         var local = "b";
         eval('print("global", global)');
         eval('print("local", local)');
    }
    test();
    

    prints:

    global a
    Uncaught ReferenceError: "local" is not defined
    

    Should eval() pick up the local variable?

  • Interesting, thanks. Looks like it should!

    I've filed an issue for it at https://github.com/espruino/Espruino/issues/2164

  • Just to add, it looks like right now it sets the scope to whatever this is

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

Scope of eval

Posted by Avatar for halemmerich @halemmerich

Actions