为什么DOM有窗口和自己? [英] Why does the DOM have both window and self?
问题描述
为什么DOM在同一件事情中有一个名为 self
的对象,另一个叫窗口
要添加到混淆窗口
有一个名为 self的属性,因此
:
Why does the DOM have an object called self
and another called window
when they are the same thing? To add to the confusion window
has a property called self
so:
window === window.self === self
为什么是这样的?我应该使用哪一个?
Why is it like this? Which one should I use?
推荐答案
self
由javascript定义环境并指向[全局]对象(但不是规范的一部分,因此可能不存在),而窗口
是DOM规范的一部分。
在大多数浏览器中,窗口
用作[全局]对象,但并不总是这样。
self
is defined by the javascript environment and points to the [global] object (but is not part of the spec, so might not be there), while window
is part of the DOM specification.
In most browsers the window
is used as the [global] object, but this is not always so.
self == window.self
不奇怪,因为它们是相同的对象 - 当 self
正在查找,它被发现作为全局对象(窗口
)的属性。所以它实际上与 window.self == window.self
相同。
That self == window.self
is not strange as they are the same object - when self
is being looked up, it is found as a property of the global object (window
). So it is in fact the same as window.self == window.self
.
如其他地方所述,可以参考 [global]
对象,你应该通过在全局中运行 var global = this;
来定义你的sef执行上下文。
As noted elsewhere, to reliable reference the [global]
object, you should define it your sef by running var global = this;
in the global execution context.
这篇关于为什么DOM有窗口和自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!