What is the Difference Between Object Constructor and Global Object

I got confused about the constructor "Global Object" (window) and "Object in JS. The confusing part is when I read sentences like this while reading about clouds, and the other when I read about objects and inheritance in JavaScript:

  • All objects in JavaScript derive from Object; all objects inherit the methods and properties of the Object.prototype object, although they can be overridden.
  • Global variables are also automatically properties of the global object (windows in browsers, etc.), ..............

We know: We know that all objects in JavaScript inherit from the "Object" object, which is the root object ! All objects in JavaScript inherit from their prototype, including the prototype of the built-in Array objects .

Array.prototype.__proto__===Object.prototype  //True

      

On the other hand, when we talk about Scopes, we have something called the global scope, which is the root scope that is itself called the global object . AND:

> Window.prototype.__proto__
Result : EventTarget { addEventListener=addEventListener(),    removeEventListener=removeEventListener(),  dispatchEvent=dispatchEvent(),  more...}

      

and

> window.__proto__

Result : Window { addEventListener=addEventListener(),  removeEventListener=removeEventListener(),  dispatchEvent=dispatchEvent(),  more...}

      

I know these are probably two completely different problems. So what is it? what leads to what? who is who?

Is there any relationship between them?

+3


source to share


1 answer


A constructor Object

is a function that creates (or converts primitives to) objects
.

Object.prototype

is a property of a function Object

that defines the root of the built-in prototype chain
. Most JavaScript objects end up inheriting from it, although you can create objects that don't.

The global object is where global variables live . Like most objects, it inherits from Object.prototype

(although this is more of a de facto standard, and the spec doesn't really require it, but most engines do). Since the constructor is Object

bound to a global variable, it also lives here.



Note that in different runtime contexts, a global object can also inherit from other objects, as long as it continues to satisfy its normal requirements. For example, in browsers, the global object inherits from either Window

(normal contexts) or WorkerGlobalScope

(Web Workers).

In many contexts, the global object is also bound to a global variable . Historically, browsers have called it Window

in common contexts, but self

(originally part of web workers) has also been standardized in HTML5. Like all global variables, these names become properties of the global object: in a normal browser, you could name it window.self.window.self.window

if you like.

+3


source







All Articles