javascript 传递 eval 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13906161/
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
javascript pass eval variables
提问by Basit
i have eval function, which needs to execute javascript from php. but i need to pass element, so i can put mouse over tips on the link user clicked on.
我有 eval 函数,它需要从 php 执行 javascript。但我需要传递元素,所以我可以将鼠标放在用户点击的链接上的提示上。
var globalEval = function globalEval(src, element) {
if (window.execScript) {
window.execScript(src);
return;
}
var fn = function(element) {
window.eval.call(window,src);
};
fn(element);
};
im using following way to pass $(this)
element
我使用以下方式传递$(this)
元素
globalEval(js_code, $(this));
// js_code is = alert(element);
i get error of undefined element, which is defined in globalEval();
how can i fix this?
我收到未定义元素的错误,这是在globalEval();
如何解决这个问题中定义的?
回答by Paul S.
This is a scoping issue as the global eval isn't invoking the code in the same scope as the variable element
. If you must use eval
even though eval is evil, you'll have to do it in a way that lets you invoke your code in the environment you want. One way to do this is to wrap it as an anonymous function which you give parameters for the environment variables of choice.
这是一个范围问题,因为全局 eval 没有在与变量相同的范围内调用代码element
。如果您必须使用eval
即使eval 是 evil,您也必须以一种允许您在所需环境中调用代码的方式来使用它。一种方法是将其包装为匿名函数,您可以为所选的环境变量提供参数。
For example
例如
window.eval.call(window,'(function (element) {'+src+'})')(element);
This means the src
string is parsed but not invoked by the eval
as it returns an anonymous function. You then invoke it, passing your data, in this case element
.
这意味着src
字符串被解析但不被调用,eval
因为它返回一个匿名函数。然后调用它,传递您的数据,在本例中element
。
Test it with var element = document.body, src = 'console.log(element.tagName)';
and you'll see it log "BODY"
. Please note that if you want to set global variables (or functions) this way they have to be stated as global explicitly (window.foobar = ...
) or they will be GCd after the anonymous function finishes.
测试它,var element = document.body, src = 'console.log(element.tagName)';
你会看到它 log "BODY"
。请注意,如果您想以这种方式设置全局变量(或函数),它们必须显式声明为全局变量(window.foobar = ...
),否则在匿名函数完成后它们将被 GCd。
回答by geocar
If allyou want to do is have this
set when you evaluate some code, try:
如果所有你想要做的是有this
,当你评估一些代码设置,尝试:
// Code you want to evaluate
var code = 'return this.whatever'
// What you want "this" bound to:
var that = { whatever: 69 }
// Now do this:
var result = new Function(code).call(that)
Using the Function constructor means you'll get what you expect; There is a lot of baggage that comes along with global eval, some of which might surprise you. Best to avoid it if you don't need it.
使用 Function 构造函数意味着你会得到你所期望的;global eval 伴随着很多包袱,其中一些可能会让您感到惊讶。如果你不需要它,最好避免它。
Now if you really wanted to call it element
, the Function constructorcan do that as well:
现在,如果您真的想调用它element
,Function 构造函数也可以这样做:
code = 'alert(element)'
var use_element = 69
result = new Function("element", code).call(this, use_element)