Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what exactly is the DOM? and what is an object as taken from the DOM?

Tags:

javascript

dom

I know a bit of Javascript and have recently started trying to tie it into HTML with the code academy course. in the following code:

function sayHello(name){
    document.getElementById("result").innerHTML = 'Hello ' + name + '!';
}

The "document" in the above code is the DOM?

That would mean that getElements is a property (function) of document, and that innerHTML is a function of the getElements function.... right?

If I am seeing this correctly, how is it possible that DOM objects have javascript properties/functions?

like image 931
Zach Smith Avatar asked Dec 01 '22 02:12

Zach Smith


1 Answers

Is document the DOM

Short answer

Yes, in the sense that it is the root of it.

Slightly longer answer

The Document Object Model (DOM) is what the browser exposes to the JavaScript runtime to allow JavaScript code to manipulate the page (its nodes and associated metadata). document is one part of the DOM.

How can the DOM have JavaScript properties

Short answer

They don't.

Slightly longer answer

The DOM is not actually managed in JavaScript (yet). It is typically managed by a separate engine altogether, written in a lower-level language like C++ or Rust (in the case of Mozilla's Servo project). The JavaScript runtime is also written in a lower-level language (again, C++ is most likely) and certain attributes of the DOM are exposed to the JavaScript runtime as if they were native JavaScript objects. The fact that they are not makes all kinds of interesting things possible ... and generally make it so that these DOM objects do not always behave as you would expect "real" JavaScript objects to behave (for example typeof querySelectorAll in IE 8 returns "object", not "function" as one would reasonably expect).

like image 196
Sean Vieira Avatar answered Dec 22 '22 00:12

Sean Vieira