变量“a”表示变量“a”。和“window.a” ?? [英] a variable "a" and "window.a" ??

查看:85
本文介绍了变量“a”表示变量“a”。和“window.a” ??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一个代码重构onload事件监听器

window.onloadListeners = new Array();

window.addOnLoadListener = function(listener){

window.onloadListeners [window.onloadListeners.length] =监听器;

}

为什么在窗口下面声明onloadListeners,addOnLoadListener?


i think bare" afunc"和window.afunc同样的事情可用

双向。


有区别吗?

解决方案

1月26日12:51 pm,gg9h0st < mn9h ... @ hotmail.comwrote:


i看到一个代码重构onload事件监听器


window.onloadListeners = new Array();

window.addOnLoadListener = function(listener){

window.onloadListeners [window.onloadListeners.length] = listener;


}为什么在窗口下面声明onloadListeners,addOnLoadListener?

i think bare" afunc"和window.afunc同样的事情可用

双向。



这是正确的,|窗口|是全局对象,因此所有全局

变量/函数都是| window |的属性反之亦然。


>

有区别吗?



当你想要动态创建全局变量/函数时,分配给window.prop特别有用。


或者如果你有一个同名的局部变量,隐藏全局

变量,访问全局变量可以通过| window |来完成。


Nickolay


文章< 11 ******************** **@j27g2000cwj.googlegroups .com> ;,

Nickolay Ponomarev< as ******* @ gmail.comwrites


< snip>


>这是正确的,|窗口|是全局对象,



< snip>


除非没有窗口。


John

-

John Harris


1月26日,12:晚上51点,gg9h0st < mn9h ... @ hotmail.comwrote:


i看到一个代码重构onload事件监听器


window.onloadListeners = new Array();

window.addOnLoadListener = function(listener){

window.onloadListeners [window.onloadListeners.length] = listener;


}



我在这里看不到重构因为onloadListeners不是内在的

事件监听器(window.onload是)有人只是制作一个友好的

onload dispatcher - 当必须以某种预先定义的顺序在

同一事件上调用几个函数时。


为什么要在窗口下面声明onloadListeners,addOnLoadListener?



我不是作者所以我只能猜测:这是个人偏好

我说的。它将使用var onloadListeners = new Array();但没有什么

犯罪在上面的方式。


我认为裸露的afunc和window.afunc同样的事情可用

双向。

有区别吗?



对于大多数情况,你可以认为它是真的 - 因为

window.a和a的行为相同。 />
在现实窗口中是一个单独的主机对象放在Global之前,

其中Global始终位于作用域链的末尾。当然,如果没有显示无窗口上下文窗口主机对象的


同样,对于大多数情况,您可以忽略此细节。它在某些特殊情况下非常重要,比如当你想要保护你的脚本免受类似Greasemonkey的程序和整体

时确保去掩盖的环境。

< http://groups.google.com/group/comp.lang.javascript/msg/65a858c19f383df0>


PS我既不支持也不支持Greasemonkey,magicFunction和

类似的运行时内容重构工具。有时虽然确保一切都能正常工作并且看起来完全如此,但是b $ b是有意或者更好的根本不起作用。


i saw a code refactorying onload event listener
window.onloadListeners=new Array();
window.addOnLoadListener=function(listener) {
window.onloadListeners[window.onloadListeners.length]=listener;
}
why declare the onloadListeners, addOnLoadListener below window?

i think bare "afunc" and "window.afunc" are same thing and available
both way.

is there a difference?

解决方案

On Jan 26, 12:51 pm, "gg9h0st" <mn9h...@hotmail.comwrote:

i saw a code refactorying onload event listener

window.onloadListeners=new Array();
window.addOnLoadListener=function(listener) {
window.onloadListeners[window.onloadListeners.length]=listener;

}why declare the onloadListeners, addOnLoadListener below window?

i think bare "afunc" and "window.afunc" are same thing and available
both way.

That''s correct, |window| is the global object, so all global
variables/functions are properties on |window| and vice versa.

>
is there a difference?

Assigning to window.prop specifically can be useful when you want to
dynamically create a global variable/function.

Or if you have a local variable with the same name, hiding the global
variable, accessing the global can be done via |window|.

Nickolay


In article <11**********************@j27g2000cwj.googlegroups .com>,
Nickolay Ponomarev <as*******@gmail.comwrites

<snip>

>That''s correct, |window| is the global object,

<snip>

Except when there isn''t a window.

John
--
John Harris


On Jan 26, 12:51 pm, "gg9h0st" <mn9h...@hotmail.comwrote:

i saw a code refactorying onload event listener

window.onloadListeners=new Array();
window.addOnLoadListener=function(listener) {
window.onloadListeners[window.onloadListeners.length]=listener;

}

I see no refactoring here because onloadListeners is not an intrinsic
event listener (window.onload is) Someone is just making a friendly
onload dispatcher - when several functions have to be called on the
same event in some pre-defined order.

why declare the onloadListeners, addOnLoadListener below window?

I''m not the author so I can only guess: it is a personal preference
matter I say. It would use var onloadListeners=new Array(); but nothing
criminal in the way above.

i think bare "afunc" and "window.afunc" are same thing and available
both way.
is there a difference?

For the majority of cases you can consider it as true - because either
window.a and a will act the same.
In the reality window is a separate host object placed before Global,
where Global is always at the end of the scope chain. Of course in case
of window-less context window host object is not presented at all.
Again, for the majority of cases you may disregard this detail. It gets
very important though in some particular circumstances, say when you
want to protect your script from Greasemonkey-like programs and overall
to ensure de-maskoned environment.
<http://groups.google.com/group/comp.lang.javascript/msg/65a858c19f383df0>

P.S. I neither support nor fight Greasemonkey, magicFunction and
similar run-time content refactoring tools. Sometimes though it is
important to ensure that everything will work and look exactly as it
was intended or better do not work at all.


这篇关于变量“a”表示变量“a”。和“window.a” ??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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