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.
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 anull
value).
So, it's not weird that you see null
in the end.
Some (technical) notes beforehand:
Object.getPrototypeOf( obj )
returns the [[Prototype]]
property of an object obj
. [[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
window
in browser-JavaScript, global
in Node.js . [[Prototype]]
and [[Class]]
properties of global
are implementation-dependent. In Chrome, the implementation of DOMWindow
looks like the one as described in this IDL . Global Scope Polluter
class. Object
. __defineGetter
, etc. properties in the console. [[Prototype]]
property of Object.prototype
is null
, see section 15.2.4 . 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); }
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
).
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.