简体   繁体   中英

What is the difference between Object constructor and Global Object

I am confused about "Global object" (window) and "Object" constructor in JS. The confusing part is when I read to similar sentences, one while reading about scopes and the other one when I was reading about objects and inheritance in JavaScript :

  1. All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden.
  2. Global variables are also automatically properties of the global object (window in browsers, etc.), ..............

What we know is : We know all the objects in JavaScript are inherited from "Object" that is the root object! All the objects in JavaScript get inherited from its prototype, including the prototype of built-in objects like " Array ".

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

On the other hand when we are talking about Scopes, we have something called global scope which is the root scope which itself called 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 that probably they are two totally different issues. So what is what? which leads to which? who is who?

Is there any relationship between them?

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

Object.prototype is a property of the Object function, which defines the root of the built-in prototype chain . Most JavaScript objects ultimately inherit from it, though it is possible to create objects that do not.

The global object is the place where global variables live . Like most objects, it inherits from Object.prototype (though this is more of a de facto standard; the spec doesn't actually require it, but most engines do it anyway). Because the Object constructor is bound to a global variable, it also lives here.

Note that in different runtime contexts, the global object can also inherit from other objects , provided that it continues to satisfy the usual requirements. In browsers, for example, the global object inherits from either Window (ordinary contexts) or WorkerGlobalScope (Web Workers).

In many contexts, the global object is also bound to a global variable . Historically browsers called this window in ordinary contexts, but self (originally part of Web Workers) was also standardized in HTML5. Like all global variables, these names become properties of the global object: in an ordinary browser, you could call it window.self.window.self.window if you really wanted to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM