10

Why window object in the browser points to window object. Mozilla Website states the reason as

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

So, my question is, how to infinitely point an object to object and how that helps to avoid doing var window = this;

window.window // returns window object
window.window.window // also returns an window
adiga
  • 34,372
  • 9
  • 61
  • 83
GMR
  • 101
  • 4

3 Answers3

9

window.window or window.window.window and so on, that's not an implementation, that's a side-effect

consider this

var win = {};
win.win = win;

now

win.win === win

and

win.win.win === win

so what they could have done is like

var window = this;

which is actually same as

this.window = this;

because all the variables declared in global scope are properties of this, so doing such thing resulted in window.window.window.window....

Ammar Hasan
  • 2,436
  • 16
  • 22
0

You can add to the Object.prototype like this:

Object.prototype.mySuperReference = {a: 42};

Now any object you create will have a mySuperReference property. For example:

Object.prototype.mySuperReference = {a: 42};
var z = {};
z.mySuperReference
Object {a: 42}

I suspect window does this because it is special -- part of the API. I haven't had a need to do something like the above in anything I've written. It is called monkey patching and it is generally a bad idea. But this would let you make the reference work just like window.

To make it behave like window, simply add a reference to window too:

window = Object.prototype.mySuperReference = {a: 42};

And now:

mySuperReference.mySuperReference.mySuperReference
Object {a: 42}
Cymen
  • 14,079
  • 4
  • 52
  • 72
  • @GMR you're welcome -- one of the reasons that this is a bad idea is it will cause issues with garbage collection (getting rid of unused variables). At least I think it will -- you'd have to test it to be sure but it is likely to confuse the GC algorithms. – Cymen Mar 04 '16 at 06:33
0

so my question how to infinitely point an object to object and how that helps to avoid doing var window = this;

Spec says

In addition to the properties defined in this specification the global object may have additional host defined properties. This may include a property whose value is the global object itself; for example, in the HTML document object model the window property of the global object is the global object itself.

So to answer your question - This global object has window as one of the property (which is again a global object). By assigning window to itself allows all window properties to have a global context available in all scopes without caching the global context - this. In short, it allows window properties and methods to be invoked globally without doing window.alert() etc.

Otherwise how would you invoke window.alert() simply as alert() since alert is not defined in a global context.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94