Is a global object an object of a class?

I want to know what a global object is in JavaScript and what class this object belongs to.

But how is Infinity , NaN and undefined part of the global object?

+4
source share
2 answers

The scope variable is defined in JavaScript by a function, and functions can be nested in other functions.

 function foo() { // new variable scope in here var a = "a"; function bar() { // another nested variable scope var b = "b"; } bar(); } 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.

+3
source

There are no real classes, but if you mean the prototype chain of a global object, the specification does not say much :

The values ​​of the [[Prototype]] and [[Class]] internal properties of the global object are implementation dependent.

([[Class]] is used, for example, window.toString() so that you can get "[object global]" .)

The three values ​​you mentioned are properties of a global object, for example:

 Infinity === window.Infinity; // true (in a browser the global object is window) 

You cannot overwrite these variables so that you can see them as literals. But in fact they are properties of a global object, and therefore you can refer to them as variables ("global variables").

+1
source

Source: https://habr.com/ru/post/1433865/


All Articles