Asynchronous Module Definition (AMD)

Dec 01, 2020javascriptjs-modules

The initial attempt was a CommonJS (CJS) transport format, then changed over time to become a module definition API.

AMD is a module format that allows module and its dependencies can be asynchronously loaded. This is particularly well suited for the browser environment where synchronous loading of modules incurs performance, usability, debugging, and cross-domain access problems.

The specification uses define function to define named or unnamed modules based on the following signature:

define(
  module_id /*optional*/,
  [dependencies] /*optional*/,
  definition function /*function for instantiating the module or object*/
);

Example of defining a named module

//Calling define with a dependency array and a factory function
define('awesome-module', ['dep1', 'dep2'], function (dep1, dep2) {
  //Define the module value by returning a value.
  return function () {}
})