Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

like image 911
P K Avatar asked Nov 04 '22 02:11

P K


1 Answers

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:

  • Object.getPrototypeOf( obj ) returns the [[Prototype]] property of an object obj.
  • The [[Class]] property of an object represents its internal class. Its name can be extracted using Object.prototype.toString.call( obj ).

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

  • Code to get the [[Class]] properties:

    var w = window;
    while (1) {
        console.log(Object.prototype.toString.call(w));
        if (w == null) break;
        w = Object.getPrototypeOf(w);
    }
    
  • 15.1 - The Global Object
  • 15.2.4 - Properties of the Object Prototype Object
  • WebCore's DOMWindow.idl What is an Interface definition language? for DOMWindow (AFAIK, it's not V8s IDL, because V8's constructors are not postfixed with Constructor).
like image 108
Rob W Avatar answered Nov 09 '22 13:11

Rob W