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

前端 未结 2 1722
粉色の甜心
粉色の甜心 2021-01-17 22:49

Line 614 of jQuery 1.7rc1:

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

Why not simply write

eval.call( window, data );?

相关标签:
2条回答
  • 2021-01-17 22:58

    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.

    0 讨论(0)
  • 2021-01-17 23:13

    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.

    0 讨论(0)
提交回复
热议问题