FrostyStraw
FrostyStraw

Reputation: 1656

Difficulty understanding APIs and the DOM

So I'm reading about APIs to learn more about them, and it seems that the usual definition is something along the lines of "an API is the interface through which you access someone else's code or through which someone else's code accesses yours," and often get examples that clear this up such as Yelp using the Google or Apple Maps API to have access to the map without needing to know any information about the implementation of the methods themselves.

But then I started reading separately about the DOM (Document Object Model), and it too is defined as an API. However, based on the previous definition and examples of API (the Yelp one), I find it hard to like..see it as an API in the same way. I understand that web browsers IMPLEMENT the DOM, but each web browser has its own implementation of it, as opposed to when you talk about the Google Maps API, which I assume has its own single implementation done by Google and is then used by other apps. So like...for a particular web browser, the web browser acts like "Google" in terms of providing the API, but who acts as...Yelp? Is it..the web page? The programmer? The language (Javascript)?

I hope my example wasn't too confusing, I just kinda want to understand who the DOM has a "Google Map API-Yelp" type of relationship with.

Upvotes: 0

Views: 88

Answers (2)

Barmar
Barmar

Reputation: 780889

An API is a specification of functions and data structures that are used by an application to interact with some other service and/or data structures.

An example is the POSIX operating system API (functions like open() and read() for accessing files, fork() and exec() for managing processes and programs, etc.). POSIX specifies the API, which allows programmers to write portable programs that will run on numerous different operating systems by using these functions.

Another example is the DOM API in web browsers. The DOM is an abstract data structure, and the DOM API is methods like getElementById() and appendChild() that act on the data structure, which the browser then displays. Each browser has its own implementation of this API, which allows Javascript applications to run across different browsers.

APIs are not inherently specific to an implementation. But in cases where service providers have a library for making use of their service, the API and its implementation are often conflated. For instance, to use the Stripe service for processing credit card payments, you would use the Stripe API that they've written. Theoretically, all the payment processors like Stripe, PayPal, SWReg, etc. could get together and define a common API, which they would all implement, but they haven't done so AFAIK.

Upvotes: 1

Marcos Silva
Marcos Silva

Reputation: 2019

I would say that whomever creates a JavaScript or HTML file is 'Yelp' and the 'Google' part would be the browser. The difference here is W3C, that standardizes the DOM API. If it wasn't for W3C, then each browser vendor could expose it's own set of DOM - a scenario that actually existed at a certain extent in the past. Thanks to W3C we can now deal with a API that is pretty consistent among different browsers.

It is as if Google and Bing decided to standardize their map API. The end result would be that Yelp could create a client that would work both with Google or Bing maps. It's still an API, even though implemented by different sources.

I hope that helps.

Upvotes: 1

Related Questions