What's the differences between AMD, CMD, CommonJS?

AMD (Asynchronous Module Definition)

AMD is a JavaScript specification that defines an API for defining code modules and their dependencies, and loading them asynchronously if desired.

The specification only defines a single function define.

1
define(id, dependencies, factory);
  • id: the module name. If not specifed, defaults to the script’s name. No relative ids.
  • dependencies: an array of dependencies, If omitted, defaults to [‘require’, ‘exports’, ‘module’]. The dependencies must be resolved prior to the execution of the module factory function, and the resolved values should be passed as arguments to the factory function with argument position corresponding to indexes in the dependencies array.
  • factory: a function that should be executed to instantiate the module or an object. It should only be executed once.

Example:

1
define('one', (require, exports, module) => () => console.log('one'))

RequireJS

References

  1. AMD
  2. RequireJS
Comments