Javascript eval 在全局范围内?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4670805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:30:14  来源:igfitidea点击:

Javascript eval on global scope?

javascriptscope

提问by wuntee

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

是否可以使用 eval 命令在全局范围内执行某些操作?例如,这将导致错误:

<script>
 function execute(x){
 eval(x);
 }

 function start(){
  execute("var ary = new Array()");
  execute("ary.push('test');");  // This will cause exception: ary is not defined
 }

</script>
<html><body onLoad="start()"></body></html>

I know the 'with' keyword will set a specific scope, but is there a keyword for the global scope? Or is it possible to define a custom scope that would allow this to work?

我知道“with”关键字将设置特定范围,但是否有全局范围的关键字?或者是否可以定义一个允许它工作的自定义范围?

<script>

 var scope = {};
 function execute(x){
  with(scope){
   eval(x);
  }
 }

 function start(){
  execute("var ary = new Array()");
  execute("ary.push('test');");  // This will cause exception: ary is not defined
 }

</script>
<html><body onLoad="start()"></body></html>

Essentially, what I am trying to do is have a global execute funciton...

本质上,我想要做的是有一个全局执行函数......

回答by orca

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

This will invoke eval using the global object, windowin browsers, as the thisargument passing any arguments you've passed into the anonymous function.

这将window在浏览器中使用全局对象调用 eval ,作为this传递您传递给匿名函数的任何参数的参数。

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

eval.call(window, x, y, z)或者eval.apply(window, arguments)如果您确定window是全局对象也是有效的。然而,这并不总是正确的。例如,process如果我没记错的话,Node.js 脚本中的全局对象就是。

回答by alex

You should be able to use eval()in global scope by calling it indirectly. However, not all browsers are currently doing this.

您应该能够eval()通过间接调用在全局范围内使用它。但是,目前并非所有浏览器都这样做。

Further Reading.

进一步阅读

回答by pyrospade

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

使用(1, eval)('...').

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

回答by Ed .

To execute some JavaScript in the global scope you can call it indirectly by using setTimeout()or if you are using jQuery, take a look at $.globalEval().

要在全局范围内执行一些 JavaScript,您可以使用setTimeout()间接调用它,或者如果您使用的是 jQuery,请查看$.globalEval()

Changing your execute method to the following will allow you to still use the 'var' keyword:

将您的执行方法更改为以下内容将允许您仍然使用“var”关键字:

function execute(x) {
    setTimeout("eval(" + x + ")", 0);
    // Or: $.globalEval(x);
}


function start() {
    try
    {
        execute("var ary = new Array()");
        execute("ary.push('test');");
    }
    catch (e)
    {
        alert(e);
    }
}


start();

回答by Chandu

I know there will be lot of comments coming in with eval is evil and I agree with that. However, to answer your question, change your start method as follows:

我知道会有很多关于 eval is evil 的评论,我同意这一点。但是,要回答您的问题,请按如下方式更改启动方法:

function start(){   
  execute("ary = new Array()");   
  execute("ary.push('test');");  // This will cause exception: ary is not defined  
} 

回答by KIM Taegyoon

Use eval.apply(null, ["code"]);.

使用eval.apply(null, ["code"]);.

eval.apply(this, ["code"]);does not work on Microsoft Script Host (cscript.exe).

eval.apply(this, ["code"]);不适用于 Microsoft 脚本宿主 (cscript.exe)。