Javascript eval on global scope?

后端 未结 6 846
长发绾君心
长发绾君心 2020-12-01 10:33

Is it possible to use the eval command to execute something with a global scope? For example, this will cause an error:



        
6条回答
  •  鱼传尺愫
    2020-12-01 11:01

    Use (1, eval)('...').

    $ node
    > fooMaker = function () { (1, eval)('foo = "bar"'); };
    [Function]
    > typeof foo
    'undefined'
    > fooMaker()
    undefined
    > typeof foo
    'string'
    > foo
    'bar'
    

提交回复
热议问题