2

Forgive my lack of knowledge about web-technology and web-related aspects and terminologies. One answer to this question states that The DOM standards define interfaces that are implemented by the objects from the DOM object tree in the browser.

What I'd really like to know is that, are the notion of interface, implementation objects etc here the same thing we are familiar with in Object-Oriented-Programming? In other words, is the DOM API an object-oriented API?

Community
  • 1
  • 1
atiyar
  • 7,762
  • 6
  • 34
  • 75
  • What do you mean by "Object-Oriented API"? It might be implemented that way, but it doesn't mean you will have access to inheritance, etc. – 4castle Aug 01 '16 at 17:58
  • @4castle, an API that I can use to program with an object-oriented language. – atiyar Aug 01 '16 at 18:03
  • Yes, I suppose it is object-oriented. Languages like JavaScript sit on top of the native code that does most of the work though. You can't make an "interface" in JavaScript. There's no compiler. – 4castle Aug 01 '16 at 18:07

2 Answers2

2

Any language where state and methods can coexist on data structures is object-oriented. Interfaces, classes and other features that are common to statically typed OO languages aren't what makes a language OO. Also, you may not have an interface feature in JS, but conceptually an interface is just a contract that an object must fulfill so nothing would prevent documenting such interfaces in JS.

The DOM API is definitely OO. Actually, DOM stands for Document Object Model. In other words, the DOM is an object-oriented solution to the problem of representing and interacting with HTML and XML documents.

However, that doesn't mean that it respects the fundamental OO principles like the SOLID principles.

plalx
  • 42,889
  • 6
  • 74
  • 90
-2

Everything in JS is an object, so the real answer to your question is yes. But I think what you are asking is what do you typically get as responses from the DOM API when you call it, and that answer is usually a HTML element as a string. That's because the DOM is just a way for scripts to connect with a web page.

It outputs HTML elements most of the time and you can test this quite easily - just use console.log(whatever); on some HTML element you have to test.

Example:

var newOne = document.getElementById('orange-text');
console.log(newOne);

Response from DOM API (typically just whatever you had in HTML):

<p id="orange-text"">This text has an orange background because of a comparison operator</p>

Now try to output that same variable, treating as an array variable calling position zero:

var newOne = document.getElementById('orange-text')[0];
console.log(newOne);

Response will be: undefined

The answer is undefined because the DOM isn't returning an object with positions you can iterate over.

serraosays
  • 7,163
  • 3
  • 35
  • 60
  • That it's not returning an array with positions, which I suspect is what the OP wants to know. – serraosays Aug 01 '16 at 18:18
  • 2
    Clarification: everything in JavaScript is an object except for primitives (like numbers, booleans, strings, undefined, and null) – 4castle Aug 01 '16 at 18:18
  • Why do so many people think "everything in javascript is an object"? Only the values in one out of the seven types are objects! All the other values are not objects! Keywords are not objects! Parentheses are not objects! And so on. – Oriol Aug 02 '16 at 02:04