Why write `window[ “eval” ].call( window, data );`

China☆狼群 提交于 2019-12-01 14:42:34

问题


Line 614 of jQuery 1.7rc1:

window[ "eval" ].call( window, data );

Why not simply write

eval.call( window, data );?


回答1:


The answer is here: Decoding jQuery,

Jim Driscoll found out that for more standards-respecting browsers, you could use eval.call(window,data), but for Chrome and IE, things are a bit different.

Internet Explorer: It seems that IE uses window.execScript(data)

Chrome: eval.call(window,data) breaks on Chrome, but window[ "eval" ].call( window, data) works on Chrome, and as well as other non-IE browsers, this is how the above workarounds based upon.




回答2:


After looking at the source, I have found this link. Have a look at the emphasized text:

Sadly, eval.call(window,src) breaks on Chrome - it complains about contexts not matching. Odd - and I was unable to Google up why this might be so. But a couple lucky guesses later, and I discovered that window.eval.call(window,src) works on all non-IE browsers. Now, when I say "var j = 1", the window[j] is the variable that's set... So, that's good. Why do we have to add the extra window. on Chrome? Not sure - I could guess, but it's too likely to be wrong.

So, window.eval is used to get globalEval work in Chrome.



来源:https://stackoverflow.com/questions/7922073/why-write-window-eval-call-window-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!