The scope variable is defined in JavaScript by a function, and functions can be nested in other functions.
function foo() {
EXCEPT, there is a global default variable area that is defined when your program starts. This is the area of ββthe main variable in which all created functions are nested by areas.
So that?
Well, each area of ββa variable has a variable object (or more precisely, a binding object). This is an internal object to which all the local variables that you create are bound.
This variable object is not directly accessible. You can add properties to it by declaring a local variable (either a function parameter or a function declaration). And you can only access properties using variable names.
Again what?
Well, the global variable scope is unique. It provides this internal variable object, automatically defining a property of the object that references the object itself. In a browser, the property is called window .
Since the property is placed in an object that returns to the object, and because the properties of the object become variables, we now have direct access to the object of the global variable.
You can verify this by noting that the window.window property is an equal reference to the window variable.
alert(window.window === window); // true
As a result, we can add a property to the window.foo = "bar"; object window.foo = "bar"; , and it will appear as a global variable alert(foo); // "bar" alert(foo); // "bar" .
Note that the only variable scope that this internal object provides is the global scope. None of the function areas disclose it.
Also note that the ECMAScript specification does not require the expansion of a global variable object. Implementation must decide.