Is there a need to prepend setTimeout and setInterval with window object?

家住魔仙堡 提交于 2019-12-11 02:45:33

问题


In the Mozilla documentation, there are some examples written with window. in front of the timer functions and some without:

function delayedAlert() {
  timeoutID = window.setTimeout(slowAlert, 2000);
}...

setTimeout(myArray.myMethod, 1000);...

window.setInterval = function (vCallback, nDelay...

I have been writing my code without window. in front without any problem so far. I want to find out if there is any situation when it would be necessary.


回答1:


If ..

  1. There is no other identifier in scope with the given name (x or window), and;
  2. There is no with binding that resolves the given name (x or window), and;
  3. The given name (x) is a property in the global scope (window)

.. then window.x and x are equivalent.

For standards-mandated global properties/functions (which must exist in the global scope of a sane web-browser environment), I do not include window. I also take care not to shadow such names.




回答2:


No you do not have to add it, the 'window' part is implicit as the root object is window. However, people continue to add it as it denotes a built-in, rather than a user defined function.



来源:https://stackoverflow.com/questions/20111612/is-there-a-need-to-prepend-settimeout-and-setinterval-with-window-object

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