-4

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 below) or Document?

By type, I mean either interface in https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model, or class in JavaScript: The Definitive Guide

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Tim
  • 1
  • 141
  • 372
  • 590

1 Answers1

6

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.
console.log(window.toString());
console.log(document.toString());
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • "a global object" usually means "an object held by a global variable", it doesn't refer to "the Global object (`window`)". – Bergi Apr 30 '19 at 07:58
  • @Bergi Yes, but OP doesn't use that terminology. OP specifically says: *In JavaScript DOM, there are global objects `window` and `document`*. so it's clear that OP is referring to *the* Global object (`window`). – Scott Marcus Apr 30 '19 at 11:28
  • Not clear to me: he mentions the `window` and `document` global variables and refers to the objects they hold – Bergi Apr 30 '19 at 11:34
  • Thanks. Is it possible to ask for the specific type of an object directly, instead of guessing and checking by `window instanceof Window`? – Tim May 01 '19 at 17:55
  • @Tim I've updated my answer to show how you can do this by checking the string version of the object. – Scott Marcus May 01 '19 at 18:38