JavaScript module specification AMD specification and CMD specification

  • 2020-09-16 07:19:49
  • OfStack

Modularity refers to the systematic decomposition of a problem according to a classification of thinking when solving a complex problem or a hybrid problem of a series. Modularity is a way of dealing with the decomposition of complex systems into manageable modules that are more reasonably structured and more maintainable. You can imagine what it means to be a software when a huge piece of system code is integrated and optimized into logical modules. For the software industry: decoupling the complexity of software systems makes it possible to "rationalize" the management, development, and maintenance of systems, no matter how large.

Modularity is the property of a software system that is broken down into a set of highly cohesive, low-coupling modules. In an ideal world, we only need to complete our own part of the core business logic code, and other aspects of the dependency can be used by directly loading modules that have already been written.

1. AMD

AMD has just one interface: define(id? ,dependencies? ,factory);

It specifies all the dependencies (dep) at the time the module is declared, and passes them as formal parameters to factory, like this:


define(['dep1','dep2'],function(dep1,dep2){...});

If there are no dependencies, define a simple module, as follows


define(function(){
  var exports = {};
  exports.method = function(){...};
  return exports;
});

There is define, which is used to wrap things, but I don't see the define keyword in the IMPLEMENTATION of Node, which is also used to wrap things, in fact, it's just an implicit Node wrapper...

RequireJS is the implementation of the AMD specification

2. CMD

Yuber wrote seajs, which follows his proposed CMD specification, which is slightly more powerful and easier to use than AMD

3. The difference between AMD and CMD

CMD is equivalent to load on demand. When defining a module, it is not necessary to make dependent modules immediately, but require can be used when it is needed, which is more convenient. On the contrary, AMD needs to make dependent modules when defining modules and introduce them into factory in the form of formal parameters

//AMD mode defines modules


define(['dep1','dep2'],function(dep1,dep2){
  // You can only use defined modules internally 
  return function(){};
});

//CMD


define(function(require,exports,module){
 // If you need something here XX Modules can be introduced 
 var xx=require('XX');
});

And SEAJS also has use function, which also needs to introduce all dependent modules first, such as


//SEAJS.Use way 
seajs.use(['dep1','dep2'],function(dep1,dep2){
  // I'm going to implement transactions here 
});

4. Plug-in support

However, there are two popular JavaScript modular systems in the world, one is CommonJS implemented by Node and the other is AMD. Many class libraries support both AMD and CommonJS, but not CMD. There may be a lot of CMD modules around the country, but they haven't caught on around the world.

The popular React and its peripheral libraries directly use CommonJS module system, npm management module and Browserify package output module.
In the near future, the new modular standard in ES6 may have to follow the new standard, and AMD and CMD may not be used much.

But at present, the front-end development does not use modular programming is really out, and the current modular programming, I still recommend using SEAJS, although many plug-ins need to add or modify 1 small piece of code to support. But once changed can be used again and again, also does not affect the support of other standards. Overall, it is more convenient and practical.

What is the difference between AMD and CMD?

After reading the above brief introduction of AMD, requireJS, CMD and seaJS, I will feel a little vague, and I always feel similar. Because requireJS is not only the pure AMD inherent thought, it also has the idea of CMD specification, but just recommended AMD specification way, seaJS is the same.

Here is Yuber's explanation of the difference between AMD and CMD:

AMD is the normalized output of RequireJS's module definition in the promotion process.

CMD is the normalized output of SeaJS's module definition in the promotion process.

Similarly, there is CommonJS Modules/2.0 specification, which is the standardized output of BravoJS in the promotion process of module definition. The & # 63;

These specifications are intended for modular development of JavaScript, particularly on the browser side.

At present, the implementation of these specifications can achieve the goal of the browser side modular development.

The difference between:

1. For dependent modules, AMD is implemented in advance and CMD is implemented in delay. Starting with RequireJS 2.0, however, RequireJS has also been changed to allow deferred execution (which can be handled differently depending on how you write it). CMD advocates as lazy as possible.

2. CMD advocates relying on the nearby, while AMD advocates relying on the front. Look at the code:

// CMD


define(function(require, exports, module) {
 var a = require('./a')
 a.doSomething()
 //  it  100  line 
 var b = require('./b') //  Dependencies can be written nearby 
 b.doSomething()
 // ...
})

// AMD is recommended by default


define(['./a', './b'], function(a, b) { //  Rely on must 1 Write it right from the start 
 a.doSomething()
 //  it  100  line 
 b.doSomething()
 // ...
})

Although AMD also supports CMD writing and passing require as a dependency, RequireJS's authors default to the above writing style, which is the default module definition in the official documentation.

3. By default, the API of AMD is one for multiple purposes, while the API of CMD is strictly differentiated and advocates the duty list 1. 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, but according to the completeness of the module system, seajs.use is provided to realize the loading and starting of the module system.

In CMD, each API is simple and pure.

4. There are some differences in details. Just look at the definition of this specification, and I won't go into more details.


Related articles: