开发者

Why do we use eval to pop a new window?

Here is the code that came across with:

function popUp(URL) {
   day = new Date();
   id = day.getTime();
   eval("page" + id + " = window.open(URL, '" + id + "','....;");
}

I don't understa开发者_如何学编程nd why we are using eval in this case, seems like a lot of website are offering this as a solution to open a popup. Why are we not simply using:

var win = window.open("...");

Examples of this code:

Variable sized popup window

Check if popup window is already open

and more on google results


Because the original developer didn't understand you could use window['page' + id] there to assign an arbitrarily global variable (no var means it goes up the scope chain, eventually assigning it to the global object, window in a browser).

It's also bad practice, as it's assigning a global variable based on the time (which could be much simpler +new Date) which is never referenced again (at least in this example).

Even it were referenced, it should return a reference, not just assign one. The function should encapsulate its state, not assign a bunch of global variables.

My guess for using the time is so no two windows have the same reference (in theory, there is no guarantee, thanks RobG).


The only difference the eval is making there is setting an arbitrary variable... which you have no way of referencing again. I have no idea why anyone in their right mind would suggest this as a solution to opening a window, as your second piece of code demonstrates the "proper" way to do so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜