是否需要在窗口对象前加上 setTimeout 和 setInterval? [英] Is there a need to prepend setTimeout and setInterval with window object?
问题描述
在 Mozilla 文档中,有一些例子用 window.
写在定时器函数前面,有些没有:
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...
我一直在写我的代码,没有前面的 window.
到目前为止没有任何问题.我想知道是否有任何必要的情况.
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.
推荐答案
If ..
- 在给定名称的范围内没有其他标识符(
x
或window
),并且; - 没有解析给定名称(
x
或window
)的with
绑定,并且; - 给定的名称 (
x
) 是全局范围 (window
) 中的一个属性
- There is no other identifier in scope with the given name (
x
orwindow
), and; - There is no
with
binding that resolves the given name (x
orwindow
), and; - The given name (
x
) is a property in the global scope (window
)
.. 那么 window.x
和 x
是等价的.
.. then window.x
and x
are equivalent.
对于标准规定的全局属性/函数(必须存在于健全的网络浏览器环境的全局范围内),我不包括window
.我也注意不要隐藏这些名字.
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.
这篇关于是否需要在窗口对象前加上 setTimeout 和 setInterval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!