window[] 和 eval() 之间的区别 - Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3294262/
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 00:46:01  来源:igfitidea点击:

Difference between window[] and eval() - Javascript

javascript

提问by Norman

I've been using both in javascript ... really don't know the difference. Googling always shows results for the "window object" or "opening a new window in javascript" so couldn't find anything there.

我一直在 javascript 中使用两者......真的不知道有什么区别。谷歌搜索总是显示“窗口对象”或“在 javascript 中打开一个新窗口”的结果,所以在那里找不到任何东西。

 eval("v"+e)
 window["v"+e]

Sometimes window works for me and at other times eval works ....

有时 window 对我有用,有时 eval 对我有用....

So what's the difference between eval() and window[] ?

那么 eval() 和 window[] 之间有什么区别?

Sorry for the newbie question though !

对不起,新手问题!

Norman

诺曼

回答by CMS

Another point that has not been addressed is that evalwill resolve the variable reference using the caller variable environment, for example:

另一个尚未解决的问题是eval将使用调用者变量 environment解析变量引用,例如:

var foo = "global";

(function () {
  var foo = "local";
  alert(eval("foo")); // alerts "local"
  alert(window["foo"]); // alerts "global"
})();

So as you can see, is not completely equivalent.

正如你所看到的, 并不完全等效。

If you simply want to reference a global variable, I would recommend you to use the window[prop]approach and avoid surprises.

如果您只是想引用一个全局变量,我建议您使用该window[prop]方法并避免意外。

回答by Paul Grime

eval() interprets arbitrary javascript statements, whereas with window you are accessing a property of the window object.

eval() 解释任意 javascript 语句,而使用 window 您正在访问 window 对象的属性。

In your example, you seem to be using a property name in both eval() and window[]. As the global scope in a browser is the same as the window object's scope they will evaluate to the same thing.

在您的示例中,您似乎在 eval() 和 window[] 中都使用了属性名称。由于浏览器中的全局作用域与 window 对象的作用域相同,因此它们的计算结果相同。

You can think of your eval("v"+e)statement as being equivalent to eval("window['v'" + e +" ]").

您可以将您的eval("v"+e)语句视为等效于eval("window['v'" + e +" ]").

回答by Marimuthu Madasamy

Both return a global variable's value. The difference is that if the global variable is undefined, you will get an error on executing eval() whereas window['variableName'] will return undefined(not an error) because accessing an undefined property is not an error but accessing an undefined variable is an error.

两者都返回一个全局变量的值。不同之处在于,如果全局变量未定义,则执行 eval() 时会出错,而 window['variableName'] 将返回 undefined(不是错误),因为访问未定义的属性不是错误,而是访问未定义的变量是一个错误。

回答by mykhal

"v"+e-> string

"v"+e-> 字符串

eval(x)-> evaluates the string x, containing javascript expression

eval(x)-> 计算字符串 x,包含 javascript 表达式

window[x]-> returns window's property with the same name, as tha value of xis. this in fact can be a global variable

window[x]-> 返回具有相同名称的窗口属性,因为它的值x是。这实际上可以是一个全局变量

therefore, when you have a global variable v1 = "foo", and e = 1, then eval("v"+e)and window["v" + e]both return "foo"

因此,当你有一个全局变量v1 = "foo", and e = 1, theneval("v"+e)window["v" + e]都返回“foo”