Search code examples
javascriptobjectwindow

Why does window have a self-reference window property


In browsers, the global window object has a window property, which is nothing but a slef-reference to the window object itself.

I can't see the utility of such a property?

The problem is that with this self-reference we can have a nested access level which will give always the same reference, why does it allow us to write window.window.window with an infinite number of window?

And if we write:

window.window.window.window.window == window

It prints:

true

Demo:

console.log(window.window.window.window.window == window);

Does anyone know the purpose of having such a property?


Solution

  • The point of having the window property refer to the object itself, was likely 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. MDN window reference