Javascript eval on global scope?

后端 未结 6 848
长发绾君心
长发绾君心 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 10:55

    (function(){
        eval.apply(this, arguments);
    }(a,b,c))
    

    This will invoke eval using the global object, window in browsers, as the this argument passing any arguments you've passed into the anonymous function.

    eval.call(window, x, y, z) or eval.apply(window, arguments) is also valid if you're certain window is the global object. This isn't always true, however. For instance, the global object in a Node.js script is process if I remember correctly.

提交回复
热议问题