最高半折刷qq各种业务和钻(家里人自己开的,尽管放心,大家多捧捧场)

sking7

导航

关于window.window

从firefox的firebug可以看出。window里引用了自身的对象。。

在MDN中找到了这样做的原因。。

-----------------------------------Quote---------------

The window property of a window object points to the window object itself. Thus the following expressions all return the same window object:

window中window的属性指向的是window对象本身,因此下面的表达式总是返回相同的window对象

window.window
window.window.window
window.window.window.window
...

In web pages, the window object is also a global object. This means that:

  1. global variables of your script are in fact properties of window:
    var global = {data: 0};
    alert(global === window.global); // displays "true"

  2. you can access built-in properties of the window object without having to type window.prefix:
    setTimeout("alert('Hi!')", 50); // equivalent to using window.setTimeout.
    alert(window === window.window); // displays "true"

The point of having the window property refer to the object itself was (probably) to make it easy to refer to the global object

window的window属性指向本身,这样做是为了让window更容易的称为全局对象

(otherwise you'd have to do a manual var window = this; assignment at the top of your script).

否则你不得不在你的script语句开头手动写这样的语句:var window=this

Another reason is that without this property you wouldn't be able to write, for example, "window.open('http://google.com/')" -

另外一个原因,没有这个属性,你就不能这么写:window.open(""),

you'd have to just use "open('http://google.com/')" instead.

你不得不这么写(open('http://google.com/)

Yet another reason to use this property is for libraries which wish to offer OOP-versions and non-OOP versions (especially JavaScript modules). If, for example, we refer to "this.window.location.href", a JavaScript module could define a property called "window" inside of a class it defined (since no global "window" variable exists for it by default) which, could be created, for example, after passing in a window object to the module class' constructor.  Thus, "this.window" inside of its functions would refer to that window object. In the non-namespaced version, "this.window" would simply refer back to "window", and also be able to get the document location without trouble. Another advantage is that the objects of such a class (even if the class were defined outside of a module) could change their reference to the window at will, as they would not be able to do if they had hard-coded a reference to "window" (yet the default in the class could still be set as the current window object).

-----------------------------------Quote---------------

摘自:https://developer.mozilla.org/en/DOM/window.window

其实看了上面的解释,可以知道原因了。。

因为script下所有的对象都是在window下的。。window.window=window.window只是在script这个范围内不再引用this来调用window下的对象

就像上面说的调用open方法

posted on 2011-11-30 23:39  G.N&K  阅读(588)  评论(0编辑  收藏  举报