Bryce Johnson
Bryce Johnson

Reputation: 6901

What is meant by "JavaScript APIs"?

My understanding of APIs is limited to the kind where I send ajax calls to a certain URL, and do something with the data I get back. For example, I can use the data from the Rotten Tomatoes API to search and manipulate data from Rotten Tomatoes, such as movie reviews and metadata for a given movie.

What I'm confused about is how that is an API at the same that jQuery is also considered an API (as are dozens of other "JavaScript APIs"). I don't seem to be sending requests back and forth to be able to use jQuery, for example, since I can include the .js file in my local file structure. Does it have to do with the built-in methods it provides me?

If so, what is the difference between a library and an API?

This is the Wikipedia definition:

An application programming interface (API) specifies how some software components should interact with each other. In addition to accessing databases or computer hardware, such as hard disk drives or video cards, an API can be used to ease the work of programming graphical user interface components.

http://en.wikipedia.org/wiki/Application_programming_interface

What does that mean in the context of JavaScript? And what does it mean in terms of how I use it? Can anyone take a moment and explain this in practical (non-abstract) layman's terms?

Upvotes: 2

Views: 243

Answers (2)

Josh
Josh

Reputation: 44916

An API is simply a published contract.

It defines the operations available, and their respective inputs and outputs if they exist.

It doesn't matter if you are calling a web service, a JavaScript function, or consuming a binary. They all have some formal published contract through which you interact.

So while a Web Service has a very strict contract because only the public methods are exposed, jQuery defines a set of methods that are intended for external public consumption.

Example:

var MyObject = function(){
   var _state = "I'm private";

   //Not intended for public consumption
   this._prefix = "Hello, my name is ";

   //Part of formal API
   this.name = "Johnson";
};

// Part of formal API
MyObject.prototype.sayHello = function(){
   console.log(this._prefix + this.name);
};

var obj = new MyObject();
obj.name = "Josh";
obj.sayHello(); // "Hello, my name is Josh"

In the above example the object has a public API that is intended for use, but it also has some members you can manipulate, but aren't intended to. There might be any number of reasons to have things like this in your object, but messing with them might have unintended consequences.

The author is free to move, rename and change the implementation details so long as he maintains the external public contract (API).

Again, an API is a documented contract... nothing more and nothing less.

Upvotes: 2

Prateek
Prateek

Reputation: 274

In laymen's terms, API is much like an added feature to anything that you need to configure to work with your own product. In context of jQuery, it provides you added functionality over native javascript. You can configure your elements to work in conjunction with this functionality to provide you extra features with lesser amount of code and effort. However a library is just a collection of some methods that can be used again and again in your software components to avoid code redundancy. However, in terms of working in real world, they both may look like same.

Upvotes: 0

Related Questions