Self-referential global object: what is it and why does it exist?

21

The window object in browsers has a window property that is self-referencing :

window.window === window; // true

And there are still other equivalent properties in browsers:

self === window; // true
top === window;  // true
self === top;    // true
self.top.window === window.top.self; // true

In Node.js, the global object is also self-referential:

global.global === global; // true

How useful are these properties, and what is the reason for their existence?

    
asked by anonymous 27.01.2014 / 02:46

2 answers

11

The need for window.window property comes from the way JavaScript references its variables. Every time an expression is created involving a identifier (ie a name) the engine of the JS looks for in a special object - named Variable Object < in> - by the key corresponding to that identifier. (this object is probably named for its function: an object that stores variables)

console.log(x + 1); // Procura por <<Variable Object>>['x'] 
                    // e depois por <<Variable Object>>['console']

When the code to be run is at the top-level , the Variable Object is equivalent to the Global Object > browser , it would be window , but in other environments it might be different). That is:

console.log(x + 1); // Procura por window['x'] 
                    // e depois por window['console']

When this code is inside a function, the Variable Object is called Activation Object , and is different for each invocation of the function:

function foo() {
    console.log(x + 1);
}

foo(); // Procura por <<Activation Object 1>>['x'] 
       // e depois por <<Activation Object 1>>['console']
foo(); // Procura por <<Activation Object 2>>['x'] 
       // e depois por <<Activation Object 2>>['console']

The programmer does not have direct access to the Activation Object, only indirectly through the attempt to access a property by name. An Activation Object defines a execution context , that is, a set of variables that are accessible for use by that code snippet. An execution context can "inherit" from another context, which it can inherit from another, and so on (for more details, see this my other answer in question

27.01.2014 / 05:50
4

self is set in the JavaScript environment and points to objeto global , but is not part of the DOM specification, and may not be present in the browser. While window is part of the DOM specification. In most browsers, window is used as objeto global but this may vary.

self == window.self is not a strange thing, since they are actually the same object (how self is used, is found to be a property of the global object ( window )), so this is the even if you use window.self == window.self .

To have a concise reference of the global object, you must define it on its own as follows (when in the global context):

var global = this;

Because of this, top ( window.top ) is used to reference the top window object, for example, if you have multiple nested frames, window.top will refer to the main page, not frames, top is used to dect whether or not you are inside a frame:

if(window.top != window.self) {
    alert("Estamos em um frame");
} 
The reason why we have window.window (property window of object window ) is to make it easy to reference objeto global , calling only window within frame, we are referencing frame, window.window we are referencing the global object, regardless of where

    
27.01.2014 / 03:09