Asynchronous Module Definition (AMD) specification. In line with the CommonJS specification. All in favor of JavaScript modular programming.
The benefits of modular programming are: 1, reusable 2, independent 3, can solve the loading dependency problem 4, can solve the problem of repeated loading
CommonJS was created by Mozilla engineer Kevin Dangoor in January 2009 as ServerJS, later renamed CommonJS to show the wider usefulness of its API. CommonJS is a set of specifications that are open to creation and approval and have many versions and implementations. CommonJS is not part of the WORK of the ECMAScript TC39 group, but some members of TC39 were involved in its development.
By comparison, CommonJS is for environments outside of the browser, such as servers and desktops, while AMD is for browsers. Or, CommonJS synchronous is the specification on the server side and AMD asynchronous is the specification on the client side.
For the server, all modules are stored on the local hard disk and can be loaded synchronously. The waiting time is the read time of the hard disk. For browsers, however, this is a big problem because modules are on the server side and the wait time can be long depending on the speed of the network, leaving the browser in a state of “suspended animation”.
Therefore, the AMD specification specifies that modules are loaded asynchronously without affecting the execution of subsequent statements. All statements that depend on this module are defined in a callback function that will not run until the load is complete.
However, CommonJS also has browser-side implementations. The idea is that all modules are now defined and indexed by ID so that they can be easily parsed in the browser environment.
The CommonJS specification states that within each module, the module variable represents the current module. This variable is an object whose exports property (module.exports) is the interface to the outside world. Loading a module loads the module.exports property of that module. The require method is used to load modules.
//example.js
var x = 5;
var addX = function (value) {
return value + x;
};
module.exports.x = x;
module.exports.addX = addX;
Copy the code
var example = require('./example.js');
console.log(example.x); / / 5
console.log(example.addX(1)); / / 6
Copy the code
Implementations of CommonJS are represented by Node.js.
This specification only defines a function “define”, which is a global variable. The function is described as:
define(id? , dependencies? , factory); Id: module name, string, optional Dependencies: module name, array, optional factory: callback function The parameters of this function correspond to the dependent module one by oneCopy the code
The sample. Definition:
//math.js
define([], function ($) {
function _add(a,b){
return a + b;
}
return {
add: _add
};
});
Copy the code
call
require(['math'].function (math) {math. Add (2.3); });Copy the code
Essentially, the define here returns a JSON element as a function, just like we normally do
var f = (function(){
function _add(){}
return {
add:_add
}
})();
Copy the code
Very similar.
Currently, there are two major Javascript libraries that implement the AMD specification: require.js and curl. As for CommonJS, it’s mostly Node.js. However, in May 2013, Isaac Z. Schlueter, author of Node.js package manager NPM, said that CommonJS was obsolete and that the Node.js kernel developers had scrapped the specification.
(2) : AMD specification for modular Programming (AMD) (Chinese version)