Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird self object in window object

I can't understand why window is under self and self is under window object.

if you go to dev-tools or Firebug and write window you got DOM window object that self is under this object. The weird part is that window is under self again!

You can write

window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self

and still you get window object!

How?!

like image 234
Mohsen Avatar asked Aug 10 '11 18:08

Mohsen


2 Answers

In a response to one of my comments:

It is a circular reference, so it doesn't end. The compiler only creates one reference. It just happens to be a reference back to the original object on which the reference was created. That's why you can do window.window.window... Like this: var obj = {}; obj.obj = obj;. There's only one reference created but it's a reference back to the original, so you can do obj.obj.obj.obj.obj.obj.obj === obj.

Comment can be seen here: Is window really global in Javascript?

like image 173
Shaz Avatar answered Nov 15 '22 05:11

Shaz


See this page, and see here. window.window is a reference to itself, so you can repeat window.window.window ... window, it will allways return the current window. self returns a reference to the current window, so here it's the same, it will allway return the DOMWindow Object.

like image 33
KooiInc Avatar answered Nov 15 '22 05:11

KooiInc