1, Commonjs
COMMONJS is a specification of JavaScript modular programming, mainly in the modular specification of the server side, a single file is a module. Each module is a separate scope, and variables defined within the module cannot be read by other modules unless defined as the properties of the global object.
There is a global method require () in COMMONJS to load the module. The COMMONJS load module is synchronized, so only the load completes to perform the subsequent operation. Like Node.js is mainly used for server programming, loaded module files are generally already existing local hard disk, so load faster, do not consider the way of asynchronous loading, so the COMMONJS specification is more applicable. However, if the browser environment, to load the module from the server, this is the asynchronous mode must be used. So there's the amd,cmd solution.
2, AMD
Commonjs is mainly for the performance of JS in the back end of the formulation, he is not suitable for the front-end. First analysis of browser-side JS and server-side JS are mainly done what things:
---------------------------------------Server Side JS | Browser-side JS-------------------------------------------
The same code needs to be executed more than once | Code needs to be distributed from one server side to multiple client execution
CPU and memory resources are bottlenecks | Bandwidth is a bottleneck
Load from disk | Load needs to be loaded over the network-
AMD (asynchronous module definition) appears, it is mainly for the front-end JS performance specifications.
The Load module is asynchronous, and the load of the module does not affect the execution of subsequent statements. All statements that rely on this module are defined in a callback function, and the callback function will not run until the load is complete.
The AMD specification uses the Define method to define modules, which introduce dependencies through arrays, and the callback function is passed in via formal parameters:
Define (["/libs/jquery"], function (jquery) {
function log () {
Alert ("Hello world!");
}
return {
Log:log
}
});
Of course AMD also allows the output module to be compatible with the COMMONJS specification:
Define (function (Require, exports, module) {
var module = require ("module");
Module.dosometing ();
Exports.do = function () {
Module.dosometing ();
}
});
AMD is the normalized output of requirejs in the process of popularization.
3, CMD
Famous Yuber wrote Seajs, is to follow his proposed CMD specification, AMD similar, but use up feel more convenient.
The difference between
cmd and AMD has the following:
① for dependent modules AMD is ahead of schedule, CMD is deferred execution. However, Requirejs from 2.0, also changed to be able to delay the execution (according to the different writing, the processing method does not pass). The
②cmd is highly reliant on proximity, AMD is highly reliant on predecessors.
//AMD
Define (['. a ', './b '], function (A, b) {
//dependencies written right from the start
a.test ();
b.test ();
});
//cmd
Define (function (Requie, exports, module) {
//dependencies can be written near
var a = require ('. a ');
a.test ();
...
//Soft dependencies
if (status) {
var = requie ('./b ');
b.test ();
}
});
Although AMD also supports CMD notation, relying on the predecessor is the default module definition for official documents.
AMD's API defaults are one when multiple uses, and CMD strictly distinguishes between the highly responsible single. For example: AMD require is divided into global and local. CMD does not have the global require, provides the seajs.use () to implement the module system loading to start. Each API in CMD is simple and pure.