AMD
AMD is the asynchronous module definition, the Chinese name is the meaning of the asynchronous module definition. It is a specification for modular development on the browser side
Because it is not JavaScript native support, the use of the AMD Specification for page development requires corresponding library functions, known as REQUIREJS, in fact, AMD is requirejs in the promotion process of the module definition of the normalized output
Requirejs mainly solves two problems
1. Multiple JS files may have dependencies, the dependent files need to be loaded into the browser earlier than the files that depend on it
2, JS load when the browser will stop the page rendering, loading more files, the page lost response time longer
See an example of using Requirejs
Define Module Mymodule.js
define ([' dependency '), function () {
var name = ' Byron ';
function Printname () {
console.log (name);
}
return {
printname:printname
};
});
Load module
require ([' mymodule '], function (my) {
my.printname ();
});
Grammar
Requirejs defines a function define, which is a global variable used to define the module
Define (ID?, dependencies, Factory); ID: Optional parameter that defines the identity of the module, if not provided, the script file name (minus extension) dependencies: is an array of module names that the current module relies on factory: Factory method, module initializes the function or object to execute. If it is a function, it should only be executed once. If it is an object, this object should be the output value of the module
Loading modules on the page using the Require function
require ([dependencies], function () {});
The Require () function accepts two parameters the first parameter is an array, which means that the second parameter of the dependent module is a callback function, which is called when the module specified by the current polygon is loaded successfully. The loaded module passes in the function as a parameter, allowing the modules to be used inside the callback function
The Require () function is loaded asynchronously when the dependent function is loaded, so that the browser does not lose its response, it specifies the callback function, and only the previous modules are loaded successfully, which solves the dependency problem.
CMD
CMD is the definition of common module definitions, the CMD specification is developed domestically, just like AMD has a Requirejs,cmd browser implementation seajs,seajs to solve the problem and Requirejs, Only when the module is defined and the module is loaded (can be said to run, parse) the timing is different
Grammar
Sea.js respect a module a file, follow the uniform wording
Define (ID?, Deps?, Factory)
Because CMD respected a file a module, so often with the file name as the module ID cmd to rely on the nearest, so generally do not write in the parameters of define dependency, write in factory
Factory is a function with three parameters, function (require, exports, module) require is a method that accepts the module identifier as the only parameter used to obtain the interface provided by other modules: require (ID) exports is an object that is used to provide an out-of-module interface modules is an object that stores some of the properties and methods associated with the current module
Look at an example:
Define module mymodule.js
define (function (Require, exports, module) {
var $ = require (' jquery.js ')
$ (' div ') . addclass (' active ');
});
Load Module
seajs.use ([' mymodule.js '], function (my) {
});
difference between AMD and CMD
On the difference between the two online can search out a bunch of articles, a simple summary
The most obvious difference is that the process of dependency is different when the module is defined.
1, AMD respected the reliance on the front, in the definition of the module must declare its dependent modules
2, CMD respected the nearest dependence, only in the use of a module and then go to require
This difference has the pros and cons, only the grammatical gap, and Requirejs and SEAJS support each other's writing
The biggest difference between AMD and CMD is that the timing of the dependent modules is different, not the timing or the way of loading.
Many people say that Requirejs is an asynchronous loading module, SEAJS is a synchronous loading module, so understanding is actually not accurate, in fact, the loading module is asynchronous, but AMD relies on the front, JS can easily know who the dependent module is, immediately loaded, and cmd nearest to the dependency, Need to use to turn the module into a string to understand the dependencies of those modules, which is also a lot of people criticized the CMD, sacrificing performance to bring the convenience of development, in fact, the parsing module time is short enough to ignore
Why do we say that the two difference is dependent on the timing of the module execution, why many people think that ADM is asynchronous, CMD is synchronous (except for the name of the reason ...). )
The same is the asynchronous loading module, AMD after the loading module completes the module will be executed, all modules loaded after execution will enter the require callback function, the execution of the main logic, the effect is dependent on the module execution sequence and writing order is not necessarily consistent, see the network speed, which first downloaded down, which first executed, But the master logic must be executed after all dependent loads have been completed.
CMD does not execute after loading a dependent module, just download it, after all the dependent modules are loaded to enter the main logic, when encountering the Require statement to execute the corresponding module, so that the module execution sequence and writing order is exactly the same
This is also a lot of people say that the AMD user experience is good, because there is no delay, the dependency module executes ahead of time, cmd performance is good, because only the user needs to execute the reason