window []和eval()之间的区别-Javascript [英] Difference between window[] and eval() - Javascript

查看:33
本文介绍了window []和eval()之间的区别-Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在javascript中使用这两种方法...真的不知道两者之间的区别.谷歌搜索总是显示窗口对象"或在javascript中打开新窗口"的结果,因此在那里找不到任何内容.

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.

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

有时,窗口对我有用,而有时,eval对...有用.

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

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

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

对不起,对于新手问题!

Sorry for the newbie question though !

诺曼

推荐答案

尚未解决的另一点是, eval 将使用调用方变量环境,例如:

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

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.

如果您只想引用全局变量,我建议您使用 window [prop] 方法并避免出现意外情况.

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

这篇关于window []和eval()之间的区别-Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆