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

前端 未结 2 1746
粉色の甜心
粉色の甜心 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.

提交回复
热议问题