Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good Practices in JavaScript API Development

What's a good approach to designing a JavaScript API?

I'm relatively new to JavaScript and learning the key good features of the language, mostly from "JavaScript: The Good Parts". Currently, I'm designing a web based tool to teach Statistics. The code base is getting unwieldly in part because I don't understand how to design a JavaScript API.

My background is in Java and C++ and am used to designing interfaces then implementing those interfaces independently. Obviously, this doesn't work well in JavaScript.

Thanks for any help and suggestions.

Update: Final version of the tool here: http://www.lock5stat.com/statkey/index.html

like image 582
Rich Avatar asked Jan 24 '11 00:01

Rich


People also ask

What is a good practice that should be followed for a REST API?

REST API Must Accept and Respond with JSON It is a common practice that APIs should accept JSON requests as the payload and also send responses back. JSON is a open and standardized format for data transfer. It is derived from JavaScript in a way to encode and decode JSON via the Fetch API or another HTTP client.

Can JavaScript be used for API?

Connect to an API using JavaScript: To make API calls using JavaScript, a reference can be made under the <script> tag to the JavaScript library which contains functions and other configuration parameters pertaining to the API should be made.


2 Answers

In my opinion, if you're using jQuery, the best solution is to divide your application into jQuery plugins and UI widgets. Plugins are great way to organize the code, you can reuse them in other applications and you can also release them separately.

You can write your applications as a tree of plugins. You can think about plugins like classes in C++/Java with aggregation but not inheritance.

Notes about code: in MAIN.bootstrapPlot function you use document.createElement, you should use jQuery like in the rest of the code, and check Canto library for Canvas.

like image 171
jcubic Avatar answered Oct 11 '22 23:10

jcubic


John Resig has a scathing critique of the NodeIterator API. Although it's a review of one specific API, it provides some insights into (at least what he believes) makes for a good tool.

like image 43
sdleihssirhc Avatar answered Oct 11 '22 23:10

sdleihssirhc