Thursday, 15 April 2010

javascript - Why does global "window" object has multi-level access -



javascript - Why does global "window" object has multi-level access -

i playing around window object looking something. noticed, global window object duplicated multiple levels.

try:

console.log(window); // returns global window object console.log(window.window); // returns global window object console.log(window.window.window); // returns global window object console.log(window.window.window.window); // returns global window object console.log(window.window.window.window.window); // returns global window object console.log(window === window.window); // returns true console.log(window.window.window === window.window.window.window); // returns true window.zombie = "zombie!"; console.log(window.zombie === window.window.zombie); // returns true

is there way can create utilize of this?

it's not multi-level, need 1 property point , have recursion.

for example:

var zombie = { fred: 'hello' }; zombie.zombie = zombie;

you can go wild , do:

alert(zombie.zombie.zombie.zombie.fred);

as specifics of why exists window, see kevin brydons answer. makes sense first level rest by-product of self referencing.

javascript

No comments:

Post a Comment