Home > Article > Web Front-end > JavaScript module specification AMD specification and CMD specification_javascript skills
Modularization means that when solving a complex problem or a series of mixed problems, the problem is systematically decomposed and processed according to a classification thinking. Modularization is a way of decomposing complex systems into manageable modules with a more reasonable code structure and higher maintainability. You can imagine how meaningful it is to the software when a huge system code is integrated, optimized and divided into highly logical modules. For the software industry: the complexity of decoupled software systems makes management, development, and maintenance "reasonable" no matter how large the system is.
There are also some professional definitions of modularity: Modularity is an attribute of a software system. This system is decomposed into a set of highly cohesive and low-coupling modules. So in an ideal world, we only need to complete part of our own core business logic code, and other dependencies can be used by directly loading modules that have been written by others.
1. AMD
AMD has only one interface: define(id?,dependencies?,factory);
It needs to specify all dependencies (dep) when declaring the module, and also pass them to the factory as formal parameters, like this:
If there are no dependencies, just define a simple module, as follows
define(function(){ var exports = {}; exports.method = function(){...}; return exports; });
There is define here, which wraps things up. Why don’t you see the define keyword in the Node implementation? It also wraps things up. In fact, it’s just implicit packaging by Node..
RequireJS implements the AMD specification
2. CMD
Uncle Yu wrote seajs, which follows the CMD specification he proposed. It is slightly more powerful than AMD and feels more convenient to use
3. The difference between AMD and CMD
CMD is equivalent to on-demand loading. When defining a module, you do not need to formulate dependent modules immediately. You can just require it when needed, which is more convenient. On the contrary, AMD requires formulating dependent modules when defining a module, and The method of formal parameters is introduced into the factory
//AMD mode definition module
define(['dep1','dep2'],function(dep1,dep2){ //内部只能使用制定的模块 return function(){}; });
//CMD
define(function(require,exports,module){ //此处如果需要某XX模块,可以引入 var xx=require('XX'); });
And SEAJS also has a use function, which requires the introduction of all dependent modules first, such as
//SEAJS.Use方式 seajs.use(['dep1','dep2'],function(dep1,dep2){ //这里实现事务 });
四、插件支持
但全球有两种比较流行的 JavaScript 模块化体系,一个是 Node 实现的 CommonJS,另外一个是 AMD。很多类库都同时支持 AMD 和 CommonJS,但是不支持 CMD。或许国内有很多 CMD 模块,但并没有在世界上流行起来。
现在比较火的 React 及周边类库,就是直接使用 CommonJS 的模块体系,使用 npm 管理模块,使用 Browserify 打包输出模块。
不久的将来 ES6 中新的模块化标准,可能就都得遵循新的标准了,什么AMD、CMD可能到时也不会怎么用了。
但是目前来说,前端开发没有用模块化编程就真的out的了,而目前的模块化编程,本人还是建议用SEAJS,虽然很多插件需要追加或修改一小块代码才能支持。但改过一次就能反复使用,也不会影响其它标准的支持。总体还算是比较方便实用的。
单独解释AMD 与 CMD 区别到底在哪里?
看了以上 AMD,requireJS 与 CMD, seaJS的简单介绍会有点感觉模糊,总感觉较为相似。因为像 requireJS 其并不是只是纯粹的AMD固有思想,其也是有CMD规范的思想,只不过是推荐 AMD规范方式而已, seaJS也是一样。
下面是玉伯对于 AMD 与 CMD 区别的解释:
AMD 是 RequireJS 在推广过程中对模块定义的规范化产出。
CMD 是 SeaJS 在推广过程中对模块定义的规范化产出。
类似的还有 CommonJS Modules/2.0 规范,是 BravoJS 在推广过程中对模块定义的规范化产出还有不少??
这些规范的目的都是为了 JavaScript 的模块化开发,特别是在浏览器端的。
目前这些规范的实现都能达成浏览器端模块化开发的目的。
区别:
1. 对于依赖的模块,AMD 是提前执行,CMD 是延迟执行。不过 RequireJS 从 2.0 开始,也改成可以延迟执行(根据写法不同,处理方式不同)。CMD 推崇 as lazy as possible.
2. CMD 推崇依赖就近,AMD 推崇依赖前置。看代码:
// CMD
define(function(require, exports, module) { var a = require('./a') a.doSomething() // 此处略去 100 行 var b = require('./b') // 依赖可以就近书写 b.doSomething() // ... })
// AMD 默认推荐的是
define(['./a', './b'], function(a, b) { // 依赖必须一开始就写好 a.doSomething() // 此处略去 100 行 b.doSomething() // ... })
虽然 AMD 也支持 CMD 的写法,同时还支持将 require 作为依赖项传递,但 RequireJS 的作者默认是最喜欢上面的写法,也是官方文档里默认的模块定义写法。
3. AMD 的 API 默认是一个当多个用,CMD 的 API 严格区分,推崇职责单一。比如 AMD 里,require 分全局 require 和局部 require,都叫 require。CMD 里,没有全局 require,而是根据模块系统的完备性,提供 seajs.use 来实现模块系统的加载启动。
CMD 里,每个 API 都简单纯粹。
4. 还有一些细节差异,具体看这个规范的定义就好,就不多说了。