Article written

  • on 28.02.2010
  • at 02:44 AM
  • by admin

The interesting javascript window object 0

Feb28

As we all know that in Javascript, the Window object is the global variable 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.

Totti

http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/digg_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/reddit_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/stumbleupon_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/delicious_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/technorati_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/google_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/facebook_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/yahoobuzz_24.png http://iamtotti.com/blog/wp-content/plugins/sociofluid/images/twitter_24.png

subscribe to comments RSS

There are no comments for this post

Please, feel free to post your own comment

* these are required fields