11

This is something I cannot find an official answer about. For some, DOM objects are JS objects, for others they differ. What is the right answer? By searching in stackoverflow, you may see controversial opinions.

For example, does the object document.body belongs to DOM API only or may it be considered as part of javascript engine too?

Does Javascript create an internal representation of it or does it just communicates with DOM to access it?

5
  • They are best considered both. For instance, document.body has a JS prototype of HTMLBodyElement. It can hold JS properties. Commented Sep 29, 2015 at 12:35
  • 1
    "javascript" is a generic term derived from JavaScript, a registered trademark. It's meaning now encompasses ECMAScript used in browsers and similar environments and also server scripting, which is not really javascript but an implementation of ECMAScript (which JavaScript is also). DOM objects were, until recently, called host objects, but in ECMAScript 2015 are called exotic objects. Commented Sep 29, 2015 at 12:35
  • @torazaburo—DOM objects, including document.body, are not required to implement prototype inheritance. Commented Sep 29, 2015 at 12:41
  • @RobG Yes, while writing that comment, I stopped to wonder if I really understood this well enough. By "not required", do you mean "usually don't", or "always do, but are not technically required to"? I have been adding things to HTMLElement.prototype all my life (yes, yes, with all the necessary prophylactic measures). Commented Sep 29, 2015 at 12:48
  • @torazaburo—not required. IE didn't implement prototype inheritance for DOM objects until about IE 9 I think. In IE 8, passing DOM objects to built–in methods as this (e.g. Array.prototype.slice.call(nodeList)) would throw an error, so the two didn't even play well together. Some host objects were implemented as ActiveX objects, famously the original XMLHttpRequest object which could only be feature tested for with try..catch. So I think Quentin has it about right. Commented Sep 29, 2015 at 12:56

4 Answers 4

11

The DOM API is a collection of standards which have implementations in a variety of programming languages.

The DOM available to JavaScript in a browser provides things in the form of JavaScript objects. Large portions of it are written in native code (so are handled by libraries not written in JavaScript but made available through a JavaScript API).

Where JavaScript leaves off and native code begins doesn't really matter, it is an implementation detail and probably varies from browser to browser. The point of having a standard API is that developers using it interact with that API and don't need to worry about how it is implemented under the hood.

Sign up to request clarification or add additional context in comments.

Comments

5

Strictly speaking, no. The JavaScript runtime has access to them, and in that capacity they can function as JavaScript objects. But they are defined in a way that is not bound to any particular language, and in most DOM implementations, they're native code. Most DOM implementations take care to make the objects function the same way you'd expect other objects in the chosen language to work, but that's not always the same way that JavaScript objects do: for example, you can't go around adding dynamic properties to objects when you're working in Java.

For most practical purposes, when you're working in the browser or in some other JavaScript runtime, yes. As I stated above, most DOM implementations try to make the DOM objects work the same way as other objects in the language, and for JavaScript, that means making them work like "real" JavaScript objects. Although IE took a while to really get this right (you need IE9+ to take full advantage), these days you can pretty much use DOM objects the same way you'd use any other JavaScript object.

Comments

2

If you inspect deeply the __proto__ of document.body for instance, you would find this :

HTMLBodyElement > HTMLElement > Element > Node > EventTarget > Object

So yes : in the browser's context, DOM objects are JS objects, this is not reciprocal of course.

But DOM API is not exclusive to Javascript, it defines interfaces which can be implemented in any languages, for instance Python has a DOM API too and in this case, DOM objects are Python objects.

Comments

0

The DOM objects are not part of the JavaScript language, they are part of the environment that is provided when JavaScript runs in a browser.

When JavaScript runs in another environment, for example in Node.js, then there is no DOM. Instead there are other objects that make up the environment that the script works with.

The DOM objects are there just for JavaScript so the script works directly with the objects, there is no extra wrapper to make them available to JavaScript.

2 Comments

Where "JavaScript" means Mozilla's implementation of ECMAScript in browsers, then DOM objects are certainly JavaScript objects. Perhaps you meant not part of the ECMAScript language?
@RobG: If you choose to think that's the meaning of "JavaScript", then, yes, that's what it would mean for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.