最近在使用sea.js。大家知道sea.js遵循CMD规范。该规范的英文说明很简洁,我试着翻译了一下,旨在交流。

Common Module Definition

通用模块定义规范

This specification addresses how modules should be written in order to be interoperable in browser-based environment. By implication, this specification defines the minimum features that a module system must provide in order to support interoperable modules.

本规范规定如何定义和书写执行在浏览器环境中的js模块,这就是说,本规范定义了一个模块的最小API,这些API提供与该模块系统的交互。

  • Modules are singletons.

模块都是单例的。

  • New free variables within the module scope should      not be introduced.

模块范围内的自由变量不应该被引入。

  • Execution must be lazy.

模块的执行是按需执行的。

Module Definition

A module is defined with define keyword, which is a function.

模块是使用关键字“define”定义的一个函数。

define(factory);

  1. The define function accepts a      single argument, the module

factory.define函数接受一个参数,模块工厂

  1. The factory may be a function or      other valid values.

Factory可能是一个函数,或是一个可验证值

  1. If factory is a function, the first      three parameters of the function, if specified, must be      "require", "exports", and "module", in that      order.

如果factory是一个函数,如果参数指定了,这个函数的前三个参数,按照顺序这三个参数是“require”,“exports”,“module”

  1. If factory is not a function, then      the module's exports are set to that object.

如果factory不是一个函数,模块的exports必须设置成一个对象。

Module Context

In a module, there are three free variables: require, exports and module.

模块中三个变量:require,exports和module

define(function(require, exports, module) {

// The module code goes here

});

The require Function

  1. require is      a function

require是一个函数

    1. require accepts a module identifier.

require接受一个模块标识

    1. require returns the exported API of the foreign module.

require返回一个外部模块对外公开的API

    1. If requested module cannot be returned, require should return null.

如果请求的模块没有返回API,require必须返回null

  1. require.async is a function require.async是一个函数
    1. require.async accepts a list of module identifiers and a       optional callback function.

require.async接受一个模块标识列表和一个回调函数

    1. The callback function receives module exports as       function arguments, listed in the same order as the order in the first       argument.

回调函数接收第i中指定的作为参数的模块列表中的所有模块的的export作为函数参数,并且顺序和该列表中的模块顺序一致。

    1. If requested module cannot be returned, the       callback should receive null correspondingly.

如果请求的模块没有任何返回,回调函数也必须相应的按照顺序接收null。

The exports Object

In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.

在模块中,还有一个参数“exports”,exports是一个对象,包含这个模块的所有API。

The module Object

  1. module.uri

The full resolved uri to the module.

模块的完整可解析的uri

  1. module.dependencies

A list of module identifiers that required by the module.

模块依赖的其他模块标识列表

  1. module.exports

The exported API of the module. It is the same as exports object.

模块公开的export对象。

Module Identifier

  1. A module identifier is and must be a literal      string.

模块标识必须是合法的字符串

  1. Module identifiers may not have a filename      extensions like .js.

模块标识不能包含文件扩展名,如.js

  1. Module identifiers should be dash-joined string,      such as foo-bar.

模块标识可以使用“-”连接,如foo-bar

  1. Module identifiers can be a relative path, like ./foo and ../bar.

模块标识可以使用相对路径,如 ./foo、../bar

Sample Code

A typical sample

math.js

define(function(require, exports, module) {

exports.add = function() {

var sum = 0, i = 0, args = arguments, l = args.length;

while (i < l) {

sum += args[i++];

}

return sum;

};

});

increment.js

define(function(require, exports, module) {

var add = require('math').add;

exports.increment = function(val) {

return add(val, 1);

};

});

program.js

define(function(require, exports, module) {

var inc = require('increment').increment;

var a = 1;

inc(a); // 2

module.id == "program";

});

Wrapped modules with non-function factory

object-data.js

define({

foo: "bar"

});

array-data.js

define([

'foo',

'bar'

]);

string-data.js

define('foo bar');

------------------------------------------------

来源:

https://github.com/cmdjs/specification/blob/master/draft/module.md

扩展:

https://github.com/seajs/seajs/issues/242

CMD规范(通用模块定义规范)(翻译)的更多相关文章

  1. CMD (sea.js)模块定义规范

    转自http://www.cnblogs.com/hongchenok/p/3685677.html   CMD 模块定义规范 在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(C ...

  2. Sea.js学习3——Sea.js的CMD 模块定义规范

    在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...

  3. CMD 模块定义规范

    在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...

  4. CMD模块定义规范

    CMD 模块定义规范 在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规 ...

  5. CMD 模块定义规范【转】

    在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...

  6. AMD模块定义规范

    AMD 即Asynchronous Module Definition,中文名是“异步模块定义”的意思.它是一个在浏览器端模块化开发的规范,服务器端的规范是CommonJS.   模块将被异步加载,模 ...

  7. JS模块规范 前端模块管理器

    一:JS模块规范(为了将js文件像java类一样被import和使用而定义为模块, 组织js文件,实现良好的文件层次结构.调用结构) A:CommonJS就是为JS的表现来制定规范,因为js没有模块的 ...

  8. UMD: 通用模块规范

    既然CommonJs和AMD风格一样流行,似乎缺少一个统一的规范.所以人们产生了这样的需求,希望有支持两种风格的“通用”模式,于是通用模块规范(UMD)诞生了.

  9. CMD 规范是不是就是 commonJS 规范?

    作者:giscafer链接:https://www.zhihu.com/question/20576942/answer/58094030来源:知乎著作权归作者所有,转载请联系作者获得授权. CMD是 ...

随机推荐

  1. IAP的几个问题

    IAP是(In-APP Purchase),苹果商店内购.先来看看它的流程:   而实际运营过程中,经常会遇到这二个问题: 只要应用玩的人比较多,基本上都会遇到过此类问题,下面是来自搜索引擎的结果: ...

  2. Python基础、文件处理

    一.概述 Python中操作文件是通过file对象来处理的,步骤: 指定文件的路径.操作的模式 对文件进行操作,读或写操作 关闭文件对象 f = open( '文件路径','访问模式') # 打开文件 ...

  3. openssl 证书操作命令

    生成Self Signed证书 # 生成一个key,你的私钥,openssl会提示你输入一个密码,可以输入,也可以不输, # 输入的话,以后每次使用这个key的时候都要输入密码,安全起见,还是应该有一 ...

  4. JDBC连接mysql数据库,添加数据

    如下:其中添加/删除/修改只是sql字符串不同 //3.要执行的字符串 String sql="INSERT INTO t_student(NAME,age,email) VALUES('x ...

  5. jvm基础笔记

    名词解释: 三类参数:标准参数(可能不会变的,java -help列出来的就是这类的),X参数(非标准化参数),XX参数(扩展参数). 所有XX 参数都以-XX开始,但后面出现的+-就不同了.+代表激 ...

  6. Code First is a bad name,这些年我们对Code First的理解都错了 !很震惊吧?

    当看到这个时,我也很震惊.估计绝大多数的人和我一样,这些年来,一直不知道Code Fisrt的真实意义.下面是一篇讲述此情况的译文,欢迎围观,若有翻译不当的地方,请指正,谢谢.如果被惊到了,请点赞!, ...

  7. Tomcat JSP提交参数中文乱码问题解决

    参考: http://blog.csdn.net/error_case/article/details/8250209 中文乱码是个老生常谈的问题,一般情况下,只要保证页面,web服务器,数据库的编码 ...

  8. URL、URI和URN三者之间的区别

    URI   统一资源标识符 Uniform Resource Identifier URL      统一资源定位符     Uniform Resource Locator URN     统一资源 ...

  9. C# 中的"yield"使用

    yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能.举例说明 usi ...

  10. 关于cookie的文章(cookie与session机制)

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...