简体   繁体   中英

Object class comes twice in prototype chain of DOMWindow?

Why do we have 2 class Object and again Object in prototype chain of window?

window --> DOMWindow --->Object --->Object ---> null

Can anyone please give me some point about this design?

Follwing is the output on chrome.

在此输入图像描述

First: The DOMWindow as shown in the console is a result of the smart Dev Tools: The constructors name is shown in this case. When you explicitly use window.__proto__.toString() , [object Object] would be shown three times.

Note about the design

To answer your question about the design, I cite the ES5 specification (emphasis is mine):

All objects have an internal property called [[Prototype]] . The value of this property is either null or an object and is used for implementing inheritance. Whether or not a native object can have a host object as its [[Prototype]] depends on the implementation. Every [[Prototype]] chain must have finite length (that is, starting from any object, recursively accessing the [[Prototype]] internal property must eventually lead to a null value).

So, it's not weird that you see null in the end.

More details

Some (technical) notes beforehand:

Table:

   toString() result:   [[Class]]   #  Additional notes
1. [object DOMWindow]    global     # The global object
2. [object Object]       Object     # [[Prototype]] of the Global object
3. [object Object]       Object     # [[Prototype]] of 2 (dummy?)
4. [object Object]       Object     # [[Prototype]] of 3 === Object.prototype
5. [object Null]         Null       # Object.prototype.__proto__ === null
  1. The global object .
    Examples: window in browser-JavaScript, global in Node.js .
  2. According to section 15.1 , the [[Prototype]] and [[Class]] properties of global are implementation-dependent. In Chrome, the implementation of DOMWindow looks like the one as described in this IDL .
  3. In Chrome, this constructor has literally no name. In Firefox, this is the Global Scope Polluter class.
    This seems to be a dummy object, hence the lack of properties.
  4. The previous object is a true instance of Object .
    This explains the logged __defineGetter , etc. properties in the console.
  5. The [[Prototype]] property of Object.prototype is null , see section 15.2.4 .

Additional references

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