This article introduces the AMD and CMD specifications of js module specifications. modularization is a way to process complicated systems into manageable modules with more reasonable code structures and higher maintainability, if you are interested in the jsamdcmd specifications, you can refer to this article to learn about modularity. It refers to solving a complex problem or a series of Mixed Problems, according to a classification of thinking, the problem is systematically decomposed to solve it. Modularization is a way to process complicated systems into manageable modules with a more reasonable code structure and higher maintainability. It can be imagined that there is a great significance for software when a huge system code is integrated and optimized and divided into logic modules. For the software industry: decoupling the complexity of software systems makes management, development, and maintenance "rational" no matter how large a system is ".
Some other major modules are defined as: modularization is the property of a software system, which is divided into a group of high cohesion and low coupling modules. Ideally, we only need to complete some of our core business logic code. Other dependencies can be directly loaded and used by the modules already written.
I. AMD
AMD has only one interface: define (id ?, Dependencies ?, Factory );
It needs to define all the dependencies (dep) when declaring the module and transmit them to the factory as a form parameter, like this:
The Code is as follows:
Define (['dep1', 'depp2'], function (dep1, dep2 ){...});
If there is no dependency, define a simple module. The following will be fine.
define(function(){ var exports = {}; exports.method = function(){...}; return exports;});
Here there is define, which wraps up things. Why didn't we see the define keyword in Node implementation? It also needs to wrap things. In fact, it's just that Node is implicitly wrapped ..
RequireJS implements AMD specifications.
Ii. CMD
YU Bo wrote seajs, which follows his CMD specifications. It is a little more powerful than AMD and it is more convenient to use.
Iii. Differences between AMD and CMD
CMD is equivalent to loading as needed. when defining a module, you do not need to create a dependency module immediately. You can use require as needed, which is more convenient. AMD is the opposite, when defining a module, You need to define the dependency module and introduce it to the factory as a form parameter.
// AMD mode definition module
Define (['dep1', 'depp2'], function (dep1, dep2) {// only the specified module can be used internally. return function (){};});
// CMD
Define (function (require, exports, module) {// if you need a XX module, you can introduce var xx = require ('xx ');});
SEAJS also has the use function, which also needs to introduce all dependent modules first, such
// SEAJS. Use method seajs. use (['dep1', 'dep2'], function (dep1, dep2) {// implement transaction here });
Iv. Plug-in support
However, there are two popular JavaScript modularization systems in the world: CommonJS implemented by Node and AMD. Many class libraries support both AMD and CommonJS, but do not support CMD. There may be many CMD modules in China, but they are not popular in the world.
Currently, the popular React and peripheral class libraries are directly using the CommonJS module system, using the npm management module and Browserify to package the output module.
In the near future, new modular standards in ES6 may have to follow new standards, and AMD and CMD may not be used at that time.
However, for the moment, front-end development does not use modular programming to really get out of it. Currently, modular programming, I suggest using SEAJS, although many plug-ins need to append or modify a small piece of code to support it. However, it can be used repeatedly after a change, without affecting the support of other standards. In general, it is more convenient and practical.
What is the difference between AMD and CMD?
After reading the above AMD, requireJS, CMD, and seaJS simple presentations are a bit vague and generally similar. For example, requireJS is not just a pure AMD concept, but also a CMD standard idea. It only recommends AMD standard methods, as does seaJS.
The following is an explanation of the difference between AMD and CMD:
AMD is the standardized output of modules defined by RequireJS during promotion.
CMD is the standardized output of modules defined by SeaJS during promotion.
Similarly, there is also the CommonJS Modules/2.0 specification, which is the standardized output of the module definition in BravoJS during the promotion process. How many ??
These specifications are designed for the modular development of JavaScript, especially on the browser side.
At present, the implementation of these specifications can achieve the goal of browser-side modular development.
Differences:
1. AMD executes the dependent modules in advance, while CMD executes the tasks in a delayed manner. However, since 2.0, RequireJS can also be converted to delayed execution (depending on the Writing Method, the processing method is different ). CMD advocates as lazy as possible.
2. CMD advocates dependency proximity, while AMD advocates dependency prefix. Check the Code:
// CMD
Define (function (require, exports, module) {var a = require ('. /A'). doSomething () // skip the 100 rows var B = require ('. /B ') // dependency can be written nearby. doSomething ()//...})
// AMD recommended by default
Define (['. /','. /B '], function (a, B) {// dependencies must be written to a at the beginning. doSomething () // 100 rows B are omitted here. doSomething ()//...})
Although AMD also supports CMD writing and transmits require as dependencies, the author of RequireJS prefers the above writing by default and is also the default module definition writing in official documents.
3. By default, AMD APIs are used for multiple purposes. CMD APIs are strictly differentiated and have a single responsibility. For example, in AMD, require is divided into global require and local require, both of which are called require. In CMD, there is no global require. Instead, seajs. use is provided based on the completeness of the module system to load and start the module system.
In CMD, every API is simple and pure.
4. There are some differences in details. Let's just look at the definition of this specification.