The interesting javascript window object

As we all know that in Client-side Javascript, the Window object is the global variable, a host object, that contains attributes of the current opened window. An interesting part is this object also has a property called ‘window‘ which refers to itself. I’m not sure why this special object is implemented that way.

alert(window);  // object Window
alert(window.window);   // object Window
alert(window.window.window);  // object Window
alert(window.window.window === window);   // true

A fun bit about this is the window object is read-only, while the self object, also referring to the current opened window, is not.

// Try assigning window to null
window = null;
alert(window);  // object Window
window.window = null;
alert(window.window); // object Window

// Try assigning self to null
window.self = null;  // only on IE 6, 7 this will returns exception: 'Not Implemented'
alert(window.self)  // on other browsers, self now is null.
alert(window)  // object Window .

This proves that self object actually is just a reference of window. And self could be assigned to point to other objects while window remains intact.

Just my 2 cents with window on Firefox :)

Totti