里面的eval(JavaScript的)设置变量变量、里面、eval、JavaScript

2023-09-10 17:58:04 作者:稳掌三界权

我在写剧本的GreaseMonkey(使用JQuery),我需要的是通过一个脚本在原来的页面中设置一些变量,像这样的:

 <脚本类型=文/ JavaScript的>
    变种RDATA = {20982211:[1,0,1],20981187:[8,0,4]};
< / SCRIPT>
 

我取的另一页元素,并尝试对eval它,把奇怪的是这不工作:

  $。获得(link_url,空,功能(数据){
   警报(1+ RDATA);
   的eval($(数据).find(脚本)文本());
   警报(2+ RDATA);
}
 

但奇怪的是Firebug控制台它工作(我只是尝试了EVAL直接在targetpage没有。获得),当我运行的脚本,但事实并非如此。它使我在这两个警报空。

任何想法?

解决方案

的ECMAScript 5重新定义评估,以便它不能添加变量绑定到封闭的词法环境。

http://whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-$c$c-are-local-to-that-$c$c-only/有关问题的评估会谈ES 3下。

  

然而,在同一时间,EVAL太强大了。内联汇编是C或C ++(至少在没有信息GCC的ASM语法要求),所以EVAL为JavaScript。在这两种情况下,一个强大的构造抑制了很多优化。即使你不关心优化和性能,EVAL的引进和删除绑定的能力,使得使用它更难推理code。

     

...

     

添加绑定的eval的能力较差。这可以使其无法说出一个名字指的是,直到运行时:

  VAR伏;
功能测试(code)
{
  的eval(code);
  返回伏;
}
 
安装监控系统后无法显示图像的原因有哪些

  

是否V IN return语句的意思是全局变量?你可以不知道不知道code EVAL编译和运行。如果code是变种V = 17;它指的是一个新的变量。如果code为/ *斗志!* /,它指的是全局变量。的eval在一个功能将deoptimize任何名称在该函数中其指的是变量在一个封闭的范围。 (不要忘记这个名字测试本身是在一个封闭的范围:如果函数返回的测试,而不是V,你不能说,测试是否所指的封闭功能,或者一个新的变量不知道code 。)

一种可能的解决问题的方法是使用不同的评价构建体,如(新功能(警报(RDATA);+ ... +;警报(RDATA);'))引入了一个完整的词法环境

i'm writing a GreaseMonkey script (using JQuery), and i need some variables that are set by a script in the original page, like this:

<script type="text/javascript"> 
    var rData = {"20982211":[1,0,1],"20981187":[8,0,4]};
</script>

I fetch this element from another page and try to eval it, put strangely this doesn't work:

$.get(link_url, null, function(data) {
   alert("1:" + rData);
   eval($(data).find("script").text());
   alert("2:" + rData);
}

The strange thing is on the firebug console it works (i just tried the eval directly on the targetpage without the .get), when i run the script though it doesn't. It gives me "null" in both alerts.

Any ideas?

解决方案

EcmaScript 5 redefined eval so that it cannot add variable bindings to the enclosing lexical environment.

http://whereswalden.com/2011/01/10/new-es5-strict-mode-support-new-vars-created-by-strict-mode-eval-code-are-local-to-that-code-only/ talks about the problems with eval under ES 3.

Yet at the same time, eval is too powerful. As inline assembly is to C or C++ (at least without the information gcc‘s asm syntax requires), so is eval to JavaScript. In both instances a powerful construct inhibits many optimizations. Even if you don’t care about optimizations or performance, eval‘s ability to introduce and delete bindings makes code that uses it much harder to reason about.

...

eval‘s ability to add bindings is worse. This can make it impossible to say what a name refers to until runtime:

var v;
function test(code)
{
  eval(code);
  return v;
}

Does the v in the return statement mean the global variable? You can’t know without knowing the code eval will compile and run. If that code is "var v = 17;" it refers to a new variable. If that code is "/* psych! */" it refers to the global variable. eval in a function will deoptimize any name in that function which refers to a variable in an enclosing scope. (And don’t forget that the name test itself is in an enclosing scope: if the function returned test instead of v, you couldn’t say whether that test referred to the enclosing function or to a new variable without knowing code.)

One possible solution to your problem is to use a different evaluation construct, e.g. (new Function('alert(rData); ' + ... + '; alert(rData);')) introduces a complete lexical environment.