In JavaScript DOM, there are global objects window and document.

What is the type of window? Is it Window?

What is the type of document? HTMLDocument (see the diagram blow) or Document?

By type, I mean either interface in, or class in JavaScript: The Definitive Guide

enter image description here


1 Answers

Scott Marcus On Best Solutions

In JavaScript DOM, there are global objects window and document.

No, not really. The window is part of the Browser Object Model (BOM), not the Document Object Model. And the document is not a Global object, it's a property of the Global window object. The reason you can access it as just document is not because document is Global, it's because window is and because of the way the JavaScript Scope Chain works, omitting window just means that it will ultimately find document in the Global window object.

The "Core" Document Object Model is a generic API for working with XML or HTML documents and because it can be used in both kinds, the document object is not specific to an HTML or XML document.

The graphic that you are showing refers to a variant of the Document Object Model, called the HTML DOM, which takes into account elements and attributes that are specific to HTML documents, and so yes, in that graphic, HTMLDocument implements the Document interface and that means that an HTMLDocument is a type of document, but it is still also implementing the Node interface.

Now finally, we have to address types vs. instances. In JavaScript there is no window or document "type", there are "objects", which window and document are. Objects though come in many different varieties and window is an instance of a Window object, while document is an instance of a Document object:

console.log(typeof window);  // The window is a type of object
console.log(window instanceof Window);  // Particularly a Window object

// You can often find out what interface the object implements
// by just logging it cast as a string.