为什么“window.alert()” over“alert()”? [英] Why "window.alert()" over "alert()"?

查看:72
本文介绍了为什么“window.alert()” over“alert()”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读c.l.j时,我注意到有些人更喜欢,甚至确实建议使用window.alert()。结束alert()。


我找不到任何技术上的理由来做出这种区分,似乎

有一个(微小的) )由于窗口本身指向全局

对象,因此是一个循环引用。


(从我正在阅读的所有内容,窗口只是一个参考)返回

全局对象,而不是单独的对象。)


这带给我两个想法:


a)我错了,窗口真的是它自己的增强对象

通过语法我不跟随


b)决定是基于风格原因,提醒编码人员

" alert()"由主机环境提供,不属于EMCA

脚本规范的一部分。

While reading c.l.j, I''ve noticed that some people prefer and indeed even
recommend the use of "window.alert()" over "alert()".

I can''t find any technical reason to make this distinction, and seems to
have a (tiny) amount overhead since window itself points to the global
object, hence, a circular reference.

(From everything I am reading, window is just a REFERENCE back to the
global object, as opposed to a separate object.)

Which brings me to two ideas:

a) Either I am wrong, and window is really its own augmented object
through a grammar I don''t follow

b) The decision is based for stylistic reasons, to remind the coder that
"alert()" is provided by the host environment and is not part of the EMCA
script specification.

推荐答案

Jeremy J Starcher meinte:
Jeremy J Starcher meinte:

在阅读clj时,我注意到有些人更喜欢,甚至确实建议使用b $ b。 window.alert()" overalert()。
While reading c.l.j, I''ve noticed that some people prefer and indeed even
recommend the use of "window.alert()" over "alert()".



如果是alert() JS将搜索范围链,直到找到

警报命名函数。它最终会在浏览器窗口对象的全局

对象中结束。如果某人(故意或

意外)在范围链中的其他地方定义了一个警报功能,那么将调用
。 " window.alert()"没有投机的余地。


Gregor

-
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur f ?? r den alpinen Raum

In case of "alert()" JS will search through the scope chain, until an
alert-named function is found. It will finally end up at the global
object, in browsers the window object. If somebody (deliberately or
accidentally) defines an alert function elsewhere in the scope chain, it
will be called instead. "window.alert()" leaves no room for speculation.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur f??r den alpinen Raum


Gregor Kofler写道:
Gregor Kofler wrote:

Jeremy J Starcher meinte:
Jeremy J Starcher meinte:

>在阅读clj时,我注意到了有些人更喜欢,甚至建议使用window.alert()。 overalert()。
>While reading c.l.j, I''ve noticed that some people prefer and indeed
even recommend the use of "window.alert()" over "alert()".



如果是alert() JS将搜索范围链,直到找到

警报命名函数。它最终将在全球


In case of "alert()" JS will search through the scope chain, until an
alert-named function is found. It will finally end up at the global



^^^^^^^^属性^^^^^^^^^^

^^^^^^^^ property ^^^^^^^^^^


对象,在浏览器窗口对象中。
object, in browsers the window object.



^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^

这个断言基于一厢情愿的想法,仍未得到任何

公共标准或详尽研究的支持。你最好不要支持
提倡它。


事实上,有证据表明相反:MSHTML正在创造阅读 - 只有

范围链中主机对象的属性才能引用元素对象

表示具有ID或名称的元素,也可以通过
$ b获得$ b`window''参考。但是全局对象被指定为本机对象,

它必须完全按照指定的方式实现[[Put]]方法;所以

不能成为'window'所引用的对象,它代表了行为


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This assertion is based on wishful thinking, still not backed up by any
public standard or exhaustive research. You would be well-advised not to
advocate it.

In fact, there is evidence to the contrary: MSHTML''s creating "read-only"
properties of a host object in the scope chain to refer to element objects
representing elements with ID or name, also being available through the
`window'' reference. But the Global Object is specified as a native object,
it has to implement the [[Put]] method exactly as it was specified; so that
cannot be the object referred to by `window'', which exhibits said behavior
instead.


如果某人(有意或无意地)在范围链中的其他地方定义了一个警报函数

,则会调用它。 " window.alert()"

没有留下任何猜测空间。
If somebody (deliberately or accidentally) defines an alert function
elsewhere in the scope chain, it will be called instead. "window.alert()"
leaves no room for speculation.



这不是原因。因为如果有人在全局对象之前在

范围链中声明了窗口(因为这不会引用全局对象的

窗口属性)然后问题仍然存在。

相反,有人不太可能这样做。


绝对肯定,人们必须特征测试和使用

global.window.alert(),其中`global'是全局定义的引用

全局对象(var global = this),相反。

PointedEars

-

var bugRiddenCrashPronePieceOfJunk =(

navigator.userAgent.indexOf(''MSIE 5'')!= -1

&& navigator.userAgent.indexOf(''Mac'')!= -1

)// Plone,register_function .js:16

That is not the reason why. Because if someone declared `window'' in the
scope chain before the Global Object (so as this would not refer to the
`window'' property of the Global Object anymore), then the issue remained.
It is instead that it is unlikely that someone would do that.

To be absolutely sure, one would have to feature-test and use
global.window.alert(), with `global'' being a globally defined reference to
the Global Object (var global = this), instead.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16


Thomas''PointedEars''Lahn< Po ********* @ web.dewrites:
Thomas ''PointedEars'' Lahn <Po*********@web.dewrites:

Gregor Kofler写道:
Gregor Kofler wrote:

>如果是alert() JS将搜索范围链,直到找到一个名为alert的命名函数。它最终将在全球
>In case of "alert()" JS will search through the scope chain, until an
alert-named function is found. It will finally end up at the global



^^^^^^^^属性^^^^^^^^^^

^^^^^^^^ property ^^^^^^^^^^


>对象,在浏览器窗口对象中。
>object, in browsers the window object.



^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


这个断言基于一厢情愿的想法,仍未得到任何

公共标准的支持或详尽无遗研究。你最好不要支持
提倡它。
This assertion is based on wishful thinking, still not backed up by any
public standard or exhaustive research. You would be well-advised not to
advocate it.



似乎全局对象'''窗口'属性在所有当前浏览器中都包含对全局对象本身的

引用,其中新的浏览器会导致自身问题而不是

做同样的事情。

没有标准,真实,除了事实上的标准。

It does seem that the global object''s "window" property holds a
reference to the global object itself in all current browsers, to the
point where a new browser would be causing itself problems by not
doing the same.
No standard, true, except de-facto standard.


事实上,有证据相反:MSHTML'创建只读

范围链中主机对象的属性以引用元素对象

表示具有ID或名称的元素,可通过

窗口参考获得。
In fact, there is evidence to the contrary: MSHTML''s creating "read-only"
properties of a host object in the scope chain to refer to element objects
representing elements with ID or name, also being available through the
`window'' reference.



它们通过全局对象同样可用。但是,它们并不是全局/窗口对象的属性,只有当通过范围链解析

时才这样。

They are equally available through the global object. They are not
ReadOnly as properties of the global/window object, though, only when
resolved through the scope chain.


但是全局对象被指定为本机对象,

它必须完全按照指定的方式实现[[Put]]方法;所以

不能成为窗口所指的对象,它表现出所谓的行为


But the Global Object is specified as a native object,
it has to implement the [[Put]] method exactly as it was specified; so that
cannot be the object referred to by `window'', which exhibits said behavior
instead.



那个,或IE没有遵循规范,并在全局对象上以不同方式实现[[Put]]

。 />

似乎MSHTML在可变分辨率

过程中有一个步骤,就好像在全局对象上面有一个对象,

包含那些element-id属性。看来直接从全局对象读取属性也会给出带有该id的

元素。这给出了两种给出相同结果的方法,

,两者不等价。


示例:


< div id =" foo"> foo< / div>

< script type =" text / javascript">

alert([foo] ,typeof foo,window.foo,typeof window.foo]);

//提醒" [object],object,[object],object"


alert(窗口中的foo);

//提醒确实


try {foo = 42; } catch(e){alert(e.message); }

// alerts"对象不支持此属性或方法


window.foo =" window.foo" ;;

alert([foo,typeof foo,window.foo,typeof window.foo]);

// alerts" [object],object,window.foo,string"


< / script>

< script type =" text / javascript">

var foo = 37 ;

alert([foo,typeof foo,window.foo,typeof window.foo]);

//提醒" 37,number,37,number"

//函数声明也会同时更改。

< / script>


使用全局对象而不是窗口 ;给出完全相同的

结果(它们是相同的对象,由==引用)。


即,范围查找出现在它之前匹配页面ID'

测试全局对象。可能有也可能没有对象持有

属性(我期待没有,而且他们是

而是通过扩展查找来解决算法直接)。


该属性不可写,但它确实遮蔽了全局对象中相同的属性




似乎id元素也是

全局/窗口对象的*属性,但这些属性不是ReadOnly

并且可以被覆盖。在此之后,foo和window.foo

解析为不同的值。


此外,如果声明了具有相同名称的变量或函数
全球范围内的
,foo不再解析为元素

id。

更有趣的是,行为取决于是否读取全局的

属性在分配之前将对象作为元素

另一个值:


< script type =" text / javascript">

// window.bar = 42; // [1]

< / script>

< div id =" bar"> bar< / div>

< script type =" text / javascript">

// alert([bar,typeof bar,window.bar,typeof window.bar]); // [2]

window.bar =" window.bar";

alert([bar,typeof bar,window.bar,typeof window.bar]) ;

//提醒" window.bar,string,window.bar,string"

< / script>


在上文中,如果取消注释[2],则最终警报将更改

更改为[object],object,window.bar,string。即,

*阅读*bar的动作因为具有该id的元素会导致稍后

引用给出相同的结果。


如果[1]也被取消注释,则最终警报会再次变回。

即,如果名称的属性已经在全局对象上

,当元素被解析并添加到DOM时,其名称为

没有特别解决,并且未在全局对象上设置。

好​​的,这是转移。关键是:有全局对象和

窗口的值。全局对象的属性是MSHTML中的同一个对象,就像在其他当前浏览器中一样。


我的赌注是你可以期待留下来是的。


....

That, or IE doesn''t follow the specification and implements [[Put]]
differently on the global object.

It does appear that MSHTML has an a step in the variable resolution
procedure that acts as if there is an object above the global object,
that holds those element-id properties. It also appears that reading
the properties directly off the global object will also give the
element with that id. This gives two ways to give the same result,
and the two are not equivalent.

Example:

<div id="foo">foo</div>
<script type="text/javascript">
alert([foo, typeof foo, window.foo, typeof window.foo]);
// alerts "[object],object,[object],object"

alert("foo" in window);
// alerts true

try { foo = 42; } catch(e) { alert(e.message); }
// alerts "Object doesn''t support this property or method"

window.foo = "window.foo";
alert([foo, typeof foo, window.foo, typeof window.foo]);
// alerts "[object],object,window.foo,string"

</script>
<script type="text/javascript">
var foo = 37;
alert([foo, typeof foo, window.foo, typeof window.foo]);
// alerts "37,number,37,number"
// a function declaration also changes both.
</script>

Using the global object instead of "window" gives exactly the same
result (they are the same object, as reported by "==").

I.e., the scope-lookup appears to match page id''s just before it
tests the global object. There may or may not be an object holding
the properties (I''m expecting that there isn''t, and that they are
instead resolved by extending the lookup algorithm directly).

The property is not writable, but it does shadow the same property
in the global object.

It appears that the id-elements are *also* properties of the
global/window object, but these properties are not ReadOnly
and can be overwritten. After this, "foo" and "window.foo"
resolves to different values.

And further, if a variable or function with the same name is declared
in the global scope, "foo" no longer resolves to the element with that
id.
Even more interesting, the behavior depends on whether one reads the
property of the global objecct as an element before assigning it
another value:

<script type="text/javascript">
// window.bar = 42; // [1]
</script>
<div id="bar">bar</div>
<script type="text/javascript">
//alert([bar, typeof bar, window.bar, typeof window.bar]); // [2]
window.bar = "window.bar";
alert([bar, typeof bar, window.bar, typeof window.bar]);
// alerts "window.bar,string,window.bar,string"
</script>

In the above, if [2] is uncommented, the final alert changes
to "[object],object,window.bar,string". I.e., the action of
*reading* "bar" as the element with that id causes later
references to give the same result.

If [1] is also uncommented, the final alert changes back again.
I.e., if a property of the name is already on the global object
when the element is parsed and added to the DOM, then its name is
not resolved specially and it is not set on the global object.
Ok, this was a diversion. The point is: There global object and
the value of the "window" property of the global object is the
same object in MSHTML, just as in the other current browsers.

My bet is that you can expect that to stay true.

....


这不是原因。因为如果有人在全局对象之前在

范围链中声明了窗口(因为这不会引用全局对象的

窗口属性)然后问题仍然存在。

相反,有人不太可能这样做。


绝对肯定,人们必须特征测试和使用

global.window.alert(),其中`global'是全局定义的引用

全局对象(var global = this),代替。
That is not the reason why. Because if someone declared `window'' in the
scope chain before the Global Object (so as this would not refer to the
`window'' property of the Global Object anymore), then the issue remained.
It is instead that it is unlikely that someone would do that.

To be absolutely sure, one would have to feature-test and use
global.window.alert(), with `global'' being a globally defined reference to
the Global Object (var global = this), instead.



这不是绝对肯定。 全球财产可以同样很好地被覆盖或遮蔽,并且甚至比某人阴影窗口更可能比b $ b更有可能。或者提醒,因为那些

是众所周知的全球属性。


绝对肯定,你应该创建一个对全局的引用
对象在需要的地方,所以你不要依赖于可以被覆盖或遮蔽的绑定




例如

var global =(function(){return this;})();

global.alert(global == global.window);


(顺便提一下,在我提供的所有浏览器中提醒真实,

包括MSHTML :)。


/ L

-

Lasse Reichstein Nielsen

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

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

That''s not being "absolutely sure". The "global" property could
equally well have been overwritten or shadowed, and it''s probably even
more likely than someone shadowing "window" or "alert", since those
are well known global properties.

To be absolutely sure, you should create a reference to the global
object at the point where it is needed, so you don''t rely on a binding
that could be overwritten or shadowed.

e.g.
var global = (function(){return this;})();
global.alert(global == global.window);

(which, by the way, alerts "true" in all the browsers I have available,
including in MSHTML :).

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


这篇关于为什么“window.alert()” over“alert()”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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