窗口对象 [英] window object

查看:54
本文介绍了窗口对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




浏览器中的全局窗口对象是否有标准?对于

示例,它支持setInterval和clearInterval等方法,以及

其他几个。


我知道w3c标准化了几个DOM的一部分,但这确实不包括窗口对象。


谢谢

Hi,

Is there a standard for the global ''window'' object in browsers? For
example, it supports methods such as setInterval and clearInterval, and
several others.

I know that w3c standardized several parts of the DOM, but this does
not include the window object.

Thank you

推荐答案

yb写道:


浏览器中的全局窗口对象是否有标准?对于
示例,它支持setInterval和clearInterval等方法,以及其他几个方法。


窗口对象不是ECMAScript语言或W3C的一部分

文档对象模型(DOM)规范 - 它属于DOM Level 0,

,在W3C DOM 1规范的引言中描述为:


''术语DOM Level 0指的是Netscape Navigator提供的HTML文档功能的混合(未正式指定)

版本3.0和Microsoft Internet Explorer 3.0版。在一些

的情况下,属性或方法已包括在内,因为

向后兼容DOM Level 0。''


< URL:http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-1176245063>

你会发现对Gecko DOM参考中窗口对象的引用:

< URL:http://developer.mozilla.org/en/docs/DOM:window>


和MSDN'''HTML和DHTML''文档:

< URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_window .asp>

全局对象是ECMAScript语言的概念(俗称

,称为''JavaScript'',是Sun的注册商标

Microsystems,Inc。),参见规范10.1.5节。


在具有窗口对象概念的用户代理中,它是同义词
带有全局对象的
。在解释全局对象时,上面的

引用说:


" ...在HTML文档对象模型中的窗口属性

全局对象本身就是全局对象。

你可以测试是否为真 - ''这个'在任何其他环境之外

指的是全局对象:


var Global = this;


现在''全局''指的是全局对象。要检查它是否指向

与窗口相同的对象:


alert(Global === window); //显示''true''

我想有可能某个地方有一个ECMAScript环境

实现了一个不是全局的窗口对象宾语。可能你会找到一个根本没有窗口对象的东西(但是如果合规事关重要的话,那么
必须有一个全局对象) )。


我知道w3c标准化了DOM的几个部分,但这确实不包括窗口对象。
Hi,

Is there a standard for the global ''window'' object in browsers? For
example, it supports methods such as setInterval and clearInterval, and
several others.
The window object is not part of either the ECMAScript Language or W3C
Document Object Model (DOM) specification - it belongs to DOM Level 0,
which is described in the introduction to the W3C DOM 1 specification as:

''The term "DOM Level 0" refers to a mix (not formally specified)
of HTML document functionalities offered by Netscape Navigator
version 3.0 and Microsoft Internet Explorer version 3.0. In some
cases, attributes or methods have been included for reasons of
backward compatibility with "DOM Level 0".''

<URL:http://www.w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-1176245063>
You will find references to the window object in the Gecko DOM reference:
<URL:http://developer.mozilla.org/en/docs/DOM:window>

and MSDN''s ''HTML and DHTML'' documentation:
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_window.asp>
The global object is a concept of the ECMAScript Language (colloquially
known as ''JavaScript'', which is a registered trademark of Sun
Microsystems, Inc.), see section 10.1.5 of the specification.

In user agents that have a concept of a window object, it is synonymous
with the global object. When explaining the global object, the above
reference says:

"...in the HTML document object model the window property of
the global object is the global object itself."
You can test whether that is true - ''this'' outside any other context
refers to the global object:

var Global = this;

Now ''Global'' refers to the global object. To check that it refers to
the same object as window:

alert( Global === window ); // shows ''true''
I guess it''s possible that somewhere there is an ECMAScript environment
that implements a window object that isn''t the global object. It''s more
likely you''ll find one that doesn''t have a window object at all (but it
must have a global object if compliance matters).

I know that w3c standardized several parts of the DOM, but this does
not include the window object.




通常不需要引用窗口对象。如果你觉得''窗口''应该包含在参考中,那么

a情况,

但不确定环境是否支持窗口对象,那么

使用上述''Global''变量,例如


var Global = this;


function someFn(){

...

Global.setTimeout(...);

...

}

有些人认为一个完全限定的引用(例如window.setTimeout())

分辨率 - 我从未试图确认这是否属实。

-

Rob



Generally there is no need to reference the window object. If there is
a circumstance that you feel ''window'' should be included in a reference,
but are uncertain whether the environment supports a window object, then
use the abovementioned ''Global'' variable, e.g.

var Global = this;

function someFn(){
...
Global.setTimeout(...);
...
}
Some believe that a fully qualified reference (e.g. window.setTimeout())
makes scripts run faster as it reduces the time spent on scope chain
resolution - I have never tried to confirm whether that is true or not.
--
Rob


RobG < RG *** @ iinet.net.au>写道:
RobG <rg***@iinet.net.au> writes:
我想有可能某个地方有一个ECMAScript
环境,它实现了一个不是全局对象的窗口对象。


Adob​​e SVG插件,IIRC。

它有一个窗口对象,但它不是全局对象。


另请注意,SVG规范试图将DOM 0的某些部分正式化为



有些人认为是完全合格的参考资料
(例如窗口。 setTimeout())使脚本运行得更快,因为它减少了在范围链解析上花费的时间 - 我从未尝试确认这是否真实。
I guess it''s possible that somewhere there is an ECMAScript
environment that implements a window object that isn''t the global
object.
The Adobe SVG plugin, IIRC.
It has a window object, but it is not the global object.

Also note that the SVG specification is trying to formalize
some parts of DOM 0.
Some believe that a fully qualified reference
(e.g. window.setTimeout()) makes scripts run faster as it reduces the
time spent on scope chain resolution - I have never tried to confirm
whether that is true or not.




应该是假的。实际上应该运行得更慢,如果有的话。

全局窗口变量名称没有不同的形式setTimeout。

两者都必须在同一作用域链中查找,以全局

对象结束。之后,window.setTimeout将在全局对象上进行另外一个属性

查找,因此它应该比使用

" setTimeout"直接。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''



It should be false. In fact is should run slower, if anything.
The global "window" variable name is no different form "setTimeout".
Both must be looked up in the same scope chain, ending at the global
object. After that, "window.setTimeout" will do one further property
lookup on the global object, so it should take longer than just using
"setTimeout" directly.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''




Lasse Reichstein Nielsen写道:

Lasse Reichstein Nielsen wrote:
应该是假的。实际上应该运行得更慢,如果有的话。
全局窗口。变量名称没有不同的形式setTimeout。
两者都必须在同一作用域链中查找,以全局
对象结束。之后,window.setTimeout将在全局对象上进行另一个属性查找,因此它应该比使用
setTimeout更长时间。直接。
It should be false. In fact is should run slower, if anything.
The global "window" variable name is no different form "setTimeout".
Both must be looked up in the same scope chain, ending at the global
object. After that, "window.setTimeout" will do one further property
lookup on the global object, so it should take longer than just using
"setTimeout" directly.




至少对于IE 5.0或更高版本以及Opera 8.0或更高版本是不正确的 -

因此为80%-90%市场上的浏览器(恩典到IE)。因此,与您的访问者相比,ECMA兼容的范围架构将是一个罕见的例子,而不是规则

。人们可以讨厌它,尖叫和哭泣:-) - 但是至少她应该知道它的价值。


对于IE和Opera而言全球及QUOT;对象和窗口对象构成两个

不同的编程范围。我再也不是在谈论一些偶然的模型,比如Adobe SVG插件等等。

我在谈论HTML页面的标准环境(XHTML是不是

支持IE并被解释为常规HTML),脚本运行

就可以了。


在这种情况下,我们有全局范围(全局变量级别全局​​

方法,如escape,encodeURI等)和窗口范围*高于*它(级别

的ID''加密HTML元素和窗口方法如setTimeout,打开

等。)

在全局范围内查找引擎搜索,如果没有找到

然后在窗口范围内。


因此,如果我们说声明

var setTimeout =''foobar'';

我们shadow window.setTimeout引用(引擎将在
中找到setTimeout,因此全局范围首先不会在上面的窗口范围内查找)。


分别如果我们宣布:

window [''foo''] =''bar'';

及以后调用alert(foo)然后

引擎会看首先在全局范围内,失败,然后在窗口范围内查找

并找到它。


对于不知情的用户,事实是:

window [''foo''] =''bar'';

alert(foo); //''bar''

可能会得出结论:全局范围和窗口范围是相同的 - 但是没有比这更真实的了 - 再次 - 至少

用于IE和Opera。


它还解释了可能的循环引用泄漏的真正机制

在IE中也是如此正如解释整体更高的内存消耗来说,这不是*内存泄漏,而是让很多人开心,特别是那些来自C ++左右的开发人员。


因为我有点厌倦了关于VK unbellyfeels ECMA的常规歇斯底里:

这里是一个样本,我不在乎是否它会触发思考过程或

双重思考会像往常一样帮助。

....

< script type =" text / javascript">

var v1 = true;


函数demo(){

var v2 = true;

alert(v1);

alert(v2);

alert(this [v2]);

alert(v3);

}


window.onload = demo;

< / script>

....

< div id =" v1"> v1< / div>

< div id =" ; v2"> v2< / div>

< div id =" v3"> v3< / div>

....


PS如果它有助于启动思考过程(我仍然希望

尽管不再关心太多): - 接下来的问题是:是

它有可能以及在IE的范围模型中如何在执行上下文中引用DOM对象
- 因此不是GC可用 - 尽管

没有一个JScript变量是否引用它?



It is not true at least for IE 5.0 or higher and Opera 8.0 or higher -
thus for 80%-90% of the browsers on the market (grace to IE). Therefore
ECMA-compliant scope schema will be rather a rare exeption than a rule
with your visitors. One can hate it, scream and cry about it :-) - but
at least she should be aware of it.

For IE and Opera "Global" object and "window" object constitute two
distinct programming scopes. Again I''m not talking about some
occasional models like Adobe SVG plugin or such.
I''m talking about the standard environment of a HTML page (XHTML is not
supported by IE and interpreted as regular HTML) with a script running
on it.

In such case we have Global scope (level of global variables ang Global
methods like escape, encodeURI etc.) and Window scope *above* it (level
of ID''entified HTML elements and window methods like setTimeout, open
etc.)

On lookup the engine search first in the Global scope, if not found
then in the window scope.

Therefore if we say declare
var setTimeout = ''foobar'';
we shadow window.setTimeout reference (engine will find "setTimeout" in
the Global scope first thus will never look in the window scope above).

Respectively if we declare:
window[''foo''] = ''bar'';
and later call alert(foo) then
engine will look first in the Global scope, fail on it, then look in
the window scope and find it.

For a non-aware user the fact that:
window[''foo''] = ''bar'';
alert(foo); // ''bar''
may lead to the conclusion that Global scope and window scope are the
same - but nothing more far from the true than that, again - at least
for IE and Opera.

It also explain the real mechanics of possible circular reference leaks
in IE as well as explains the overall higher memory consumption by
JScript - which is *not* a memory leak but makes nervious many
developers, specially ones coming from a C++ or so background.

As I''m kind of tired of regular hysterics about "VK unbellyfeels ECMA":
here a sample and I don''t care if it trigs the thinking process or the
doublethink will help as usual.
....
<script type="text/javascript">
var v1 = true;

function demo() {
var v2 = true;
alert(v1);
alert(v2);
alert(this[v2]);
alert(v3);
}

window.onload = demo;
</script>
....
<div id="v1">v1</div>
<div id="v2">v2</div>
<div id="v3">v3</div>
....

P.S. If it helped to start the thinking process (which I still hope
despite don''t care too much anymore):- the next question would be: "Is
it possible and how in IE''s scope model that a DOM object is still
referenced in the execution context - thus not GC-available - despite
no one JScript variable holds a reference to it?"


这篇关于窗口对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆