Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

This article needs an editorial review. How you can help.

Summary

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
  ...

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 (otherwise you'd have to do a manual var window = this; assignment at the top of your script).

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

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).

Specifications

Specification Status Comment
WHATWG HTML Living Standard
The definition of 'Window.window' in that specification.
Living Standard No difference from the latest snapshot HTML5.1
HTML5.1
The definition of 'Window.window' in that specification.
Working Draft No difference from the HTML5
HTML5
The definition of 'Window.window' in that specification.
Recommendation First snapshot containing the definition of Window.window.

Document Tags and Contributors

 Last updated by: momdo,