AMD
Stands for Asynchronous Module Definition. AMD API specifies a mechanism for defining modules and their dependencies. The beauty is that those modules then can be loaded asynchronously without worrying about the loading order. The AMD API allows code to be highly decoupled and extremely reusable. A module can be anything; a class, a function, an object or any snippet of code. The API simply consists of a single function with the signature below:
define(id?, dependancies?, factory);
id and dependancies are optional. The factory is a function used to instantiate a module. if the id argument is not specified the module name is it's file location. If the dependancy argument is not specified then the module has no dependancies.
AMD provides the following benefits:
- Asynchronous by nature
- Dependencies are easy to identify
- Avoids global variables
- Can be lazy loaded
- Is easily portable
- Can load more than JavaScript files
- Has powerful plugin support
- Supports script fall backs (e.g. when OMG or CDN is down)
- Can be easily configured
- Can load multiple versions of the same library
RequiteJS
RequireJS is an AMD loader, which just means it is an implementation of the AMD API or specification. You can use RequireJS in your project to get the benefits the AMD API provides.
npm install -g requirejs
node r.js -o build.js