简体   繁体   English

Object构造函数和Global Object之间有什么区别

[英]What is the difference between Object constructor and Global Object

I am confused about "Global object" (window) and "Object" constructor in JS. 我对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 : 令人困惑的部分是当我读到类似的句子时,一个是在阅读有关范围的时候,另一个是在我阅读JavaScript中的对象和继承时:

  1. All objects in JavaScript are descended from Object; JavaScript中的所有对象都来自Object; all objects inherit methods and properties from Object.prototype, although they may be overridden. 所有对象都从Object.prototype继承方法和属性,尽管它们可能被覆盖。
  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! 我们知道的是:我们知道JavaScript中的所有对象都是从作为对象的“对象”继承而来的! All the objects in JavaScript get inherited from its prototype, including the prototype of built-in objects like " Array ". JavaScript中的所有对象都从其原型继承而来,包括像“ 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 . 另一方面,当我们讨论Scopes时,我们有一个叫做全局作用域的东西,它本身就是全局对象的根作用域。 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构造函数是一个创建(或将原语转换为)对象的函数

Object.prototype is a property of the Object function, which defines the root of the built-in prototype chain . Object.prototypeObject函数的一个属性,它定义了内置原型链的根 Most JavaScript objects ultimately inherit from it, though it is possible to create objects that do not. 大多数 JavaScript对象最终都是从它继承的,尽管可以创建没有的对象。

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). 像大多数对象一样,它继承自Object.prototype (虽然这更像是事实上的标准;规范实际上并不需要它,但大多数引擎无论如何都会这样做)。 Because the Object constructor is bound to a global variable, it also lives here. 因为Object构造函数绑定到全局变量,所以它也存在于此处。

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). 例如,在浏览器中,全局对象继承自Window (普通上下文)或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. 历史上,浏览器在普通上下文中调用此window ,但self (最初是Web Workers的一部分)也在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. 与所有全局变量一样,这些名称成为全局对象的属性:在普通浏览器中,如果您真的想要,可以将其称为window.self.window.self.window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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