1 概述jquery
CMD(Common Module Definition)是国内大牛玉伯在开发SeaJS的时候提出来的,属于CommonJS的一种规范,根据浏览器的异步环境作了本身的实现。它和 AMD 很类似,尽可能保持简单,并与 CommonJS 和 Node.js 的 Modules 规范保持了很大的兼容性。git
2 define方法:定义模块github
在CMD中,一个模块就是一个文件,格式为:define( factory );define 是一个全局函数,用来定义模块。数组
2.1 factory 函数浏览器
define 接受 factory 参数,factory 能够是一个函数,也能够是一个对象或字符串。异步
factory 为对象、字符串时,表示模块的接口就是该对象、字符串。好比能够以下定义一个 JSON 数据模块:async
define({ "foo": "bar" });
也能够经过字符串定义模板模块:函数
define('I am a template. My name is {{name}}.');
factory 为函数时,表示是模块的构造方法。执行该构造方法,能够获得模块向外提供的接口。factory方法在执行时,默认会传入三个参数:require、exports 和 module:ui
define(function(require, exports, module) { // 模块代码 });
define也能够接受两个以上的参数,字符串id为模块标识,数组deps为模块依赖,格式为:define( id?, deps?, factory );具体使用以下:this
define('hello', ['jquery'], function(require, exports, module) { // 模块代码 });
可是,带 id 和 deps 参数的 define 用法不属于 CMD 规范。CMD推崇一个文件一个模块,因此常常就用文件名做为模块id。CMD推崇依赖就近,因此通常不在define的参数中写依赖,而在factory中写。
2.2 define.cmd 属性
defin.cmd属性是一个空对象,可用来断定当前页面是否有 CMD 模块加载器,其用法跟AMD的denfine.amd类似,写法以下:
if (typeof define === "function" && define.cmd) { // 有 Sea.js 等 CMD 模块加载器存在 }
3 require方法:加载模块
require 是 factory 函数的第一个参数。require 是一个方法,接受 模块标识 做为惟一参数,用来获取其余模块提供的接口。
define(function(require, exports) { // 获取模块 a 的接口 var a = require('./a'); // 调用模块 a 的方法 a.doSomething(); });
3.1 require.async (id, callback)
require.async 方法用来在模块内部异步加载模块,并在加载完成后执行指定回调。callback 参数可选。
define(function(require, exports, module) { // 异步加载模块,在加载完成时,执行回调 require.async(['./c', './d'], function(c, d) { c.doSomething(); d.doSomething(); }); });
require 是同步往下执行,require.async 则是异步回调执行。require.async 通常用来加载可延迟异步加载的模块。
define(function(require, exports, module) { // 异步加载模块,在加载完成时,执行回调 require.async(['./c', './d'], function(c, d) { c.doSomething(); d.doSomething(); }); });
3.2 require.resolve(id)
使用模块系统内部的路径解析机制来解析并返回模块路径。该函数不会加载模块,只返回解析后的绝对路径。
define(function(require, exports) { console.log(require.resolve('./b')); // ==> http://example.com/path/to/b.js });
这能够用来获取模块路径,通常用在插件环境或需动态拼接模块路径的场景下。
exports 是一个对象,用来向外提供模块接口。
define(function(require, exports) { // 对外提供 foo 属性 exports.foo = 'bar'; // 对外提供 doSomething 方法 exports.doSomething = function() {}; });
除了给 exports 对象增长成员,还能够使用 return 直接向外提供接口。
define(function(require) { // 经过 return 直接提供接口 return { foo: 'bar', doSomething: function() {} }; });
须要特别注的是,不能直接给 exports
赋值,例以下面是错误的写法:
define(function(require, exports) { // 错误用法!!! exports = { foo: 'bar', doSomething: function() {} }; });
exports 仅仅是 module.exports 的一个引用。而模块导出的时候,真正导出的是module.exports,而不是exports。在 factory 内部给 exports 从新赋值时, exports 就再也不指向module.exports 了。所以给 exports 赋值是无效的,正确的写法是用 return 或者给 module.exports 赋值:
define(function(require, exports, module) { // 正确写法 module.exports = { foo: 'bar', doSomething: function() {} }; });
module 是一个对象,上面存储了与当前模块相关联的一些属性和方法。
5.1 module.id
模块的惟一标识。
define('id', [], function(require, exports, module) { // 模块代码 });
上面代码中,define 的第一个参数就是模块标识。
5.2 module.uri
根据模块系统的路径解析规则获得的模块绝对路径。
define(function(require, exports, module) { console.log(module.uri); // ==> http://example.com/path/to/this/file.js });
通常状况下(没有在 define 中手写 id 参数时),module.id 的值就是 module.uri,二者彻底相同。
5.3 module.dependencies
dependencies 是一个数组,表示当前模块的依赖。
5.4 module.exports
表示当前模块对外输出的接口,其余文件加载该模块,实际上就是读取module.exports变量。传给 factory 构造方法的 exports 参数是 module.exports 对象的一个引用。只经过 exports 参数来提供接口,有时没法知足开发者的全部需求。 好比当模块的接口是某个类的实例时,须要经过 module.exports 来实现:
define(function(require, exports, module) { // exports 是 module.exports 的一个引用 console.log(module.exports === exports); // true // 从新给 module.exports 赋值 module.exports = new SomeClass(); // exports 再也不等于 module.exports console.log(module.exports === exports); // false });
注意:对 module.exports 的赋值须要同步执行,不能放在回调函数里。下面这样是不行的:
// x.js define(function(require, exports, module) { // 错误用法 setTimeout(function() { module.exports = { a: "hello" }; }, 0); });
在 y.js 里有调用到上面的 x.js:
// y.js define(function(require, exports, module) { var x = require('./x'); // 没法马上获得模块 x 的属性 a console.log(x.a); // undefined });
这就是 CMD 模块定义规范的全部内容。下一篇咱们开始介绍CMD规范的产物SeaJs加载器,进一步了解CMD规范的具体实现。
参考连接