Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are DOM objects javascript objects?

Tags:

javascript

dom

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?

like image 528
Unknown developer Avatar asked Sep 29 '15 12:09

Unknown developer


People also ask

Is DOM a JavaScript object?

Usually it refers to JavaScript, even though modeling HTML, SVG, or XML documents as objects are not part of the core JavaScript language. The DOM represents a document with a logical tree. Each branch of the tree ends in a node, and each node contains objects. DOM methods allow programmatic access to the tree.

Is the DOM an object?

What is the DOM? The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.

Is DOM part of JavaScript?

In Node for example, there wouldn't necessarily be a 'window'. The DOM API is not part of the JavaScript language but it is exposed to JavaScript by the browser.

How do we get DOM object in JavaScript?

The easiest way to find an HTML element in the DOM, is by using the element id.


2 Answers

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.

like image 102
Quentin Avatar answered Nov 10 '22 00:11

Quentin


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.

like image 26
The Spooniest Avatar answered Nov 10 '22 01:11

The Spooniest