公共模块定义/草案(Common Module Definition / draft - CMD草案)
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.
- 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(factory);
- The
definefunction accepts a single argument, the module factory. - The
factorymay be a function or other valid values. - If
factoryis a function, the first three parameters of the function, if specified, must be "require", "exports", and "module", in that order. - If
factoryis not a function, then the module's exports are set to that object.
Module Context
In a module, there are three free variables: require, exports and module.
define(function(require, exports, module) {
// The module code goes here
});
The require Function
requireis a functionrequireaccepts a module identifier.requirereturns the exported API of the foreign module.- If requested module cannot be returned,
requireshould return null.
require.asyncis a functionrequire.asyncaccepts a list of module identifiers and a optional callback function.- The callback function receives module exports as function arguments, listed in the same order as the order in the first argument.
- If requested module cannot be returned, the callback should receive null correspondingly.
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.
The module Object
module.uriThe full resolved uri to the module.
module.dependenciesA list of module identifiers that required by the module.
module.exportsThe exported API of the module. It is the same as
exportsobject.
Module Identifier
- A module identifier is and must be a literal string.
- Module identifiers may not have a filename extensions like
.js. - Module identifiers should be dash-joined string, such as
foo-bar. - Module identifiers can be a relative path, like
./fooand../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');
译文:
本规范阐述了如何编写模块,以便在基于浏览器的环境中进行互操作。本规范定义了模块系统必须提供的最小功能,以支持互操作模块。
- 模块应该是单例。
- 不应引入模块作用域范围内的新的自由变量。(模块作用域范围内不应引入新的自由变量)(其他模块不应引入模块作用域范围内的新的自由变量)
- 执行必须是懒惰的。
模块定义
一个模块就是一个函数,使用“define”关键字定义。
例如:
define(factory);
define函数接受单个参数,即模块工厂。
工厂可能是一个函数或其他有效值。
如果factory是一个函数,函数的前三个参数(如果指定的话)必须是“require”,“exports”和“module”。
如果工厂不是一个函数(不是函数那必然就是对象或者基本类型了),那么模块的export属性应该设置为该对象(这里这个“对象”两个字,意思是“那个传入模块的单个参数,即模块工程,因为它不是函数,那么可能是js对象或者js基本类型)。
模块上下文
一个模块中有三个自由变量:require,exports 和 module。
例如:
define(function(require, exports, module) {
// The module code goes here
});
require函数
1. ”require“是一个这样的函数:
require函数接收一个模块标识符(模块标识符也叫模块id)。require函数返回外部模块的导出API(”导出API“是用来导出内容给外部模块使用的)。- 如果无法返回请求的模块,
require函数将返回null。
2. ”require.async“ 是一个这样的函数:
require.async接收一个模块Id列表和一个可选的回调函数。- 回调函数接收模块导出作为函数参数,按照与第一个参数中的顺序相同的顺序列出。
- 如果不能返回请求的模块,则回调应该相应地收到null。
exports对象
每个模块中都有个名叫"exports"的自由变量,这是一个模块可以在模块执行时添加模块API的对象。
module对象
1. module.uri:完整解析的模块URI(模块URI的全路径)。
2. module.dependencies:模块请求的标识符(模块id)列表。
3. module.exports:模块的导出API(”导出API“是”用来导出什么东西的API“)。 它与export对象相同。
模块标识符(模块id)
- 模块的标识符(模块id)必须是字面量字符串。
模块标识符(模块id)不能有类似 .js 的文件名扩展。- 模块标识符(模块id)应该是加前/后缀的字符串,比如:foo-bar。
- 模块标识符(模块id)可以是相对路径,例如:
./foo和../bar.。
示例代码
一个典型的例子:
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); //
module.id == "program";
});
使用非函数的工厂包装模块
object-data.js
define({
foo: "bar"
});
array-data.js
define([
'foo',
'bar'
]);
string-data.js
define('foo bar');
公共模块定义/草案(Common Module Definition / draft - CMD草案)的更多相关文章
- 【SysML】模块定义图(BDD, Block Definition Diagram)
一.引言 SysML中的模块定义图,英文为 “Block Definition Diagram”,简称BDD,是系统建模过程中最为常见的图之一,BDD是一种结构图,它主要对系统的结构组成以及组成元素间 ...
- CMD规范(通用模块定义规范)(翻译)
最近在使用sea.js.大家知道sea.js遵循CMD规范.该规范的英文说明很简洁,我试着翻译了一下,旨在交流. Common Module Definition 通用模块定义规范 This spec ...
- Sea.js学习3——Sea.js的CMD 模块定义规范
在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...
- CMD 模块定义规范
在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...
- CMD模块定义规范
CMD 模块定义规范 在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规 ...
- CMD (sea.js)模块定义规范
转自http://www.cnblogs.com/hongchenok/p/3685677.html CMD 模块定义规范 在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(C ...
- CMD 模块定义规范【转】
在 Sea.js 中,所有 JavaScript 模块都遵循 CMD(Common Module Definition) 模块定义规范.该规范明确了模块的基本书写格式和基本交互规则. 在 CMD 规范 ...
- 理解JS中的模块规范(CommonJS,AMD,CMD)
随着互联网的飞速发展,前端开发越来越复杂.本文将从实际项目中遇到的问题出发,讲述模块化能解决哪些问题,以及如何使用 Sea.js 进行前端的模块化开发. 恼人的命名冲突 我们从一个简单的习惯出发.我做 ...
- JS JavaScript模块化(ES Module/CommonJS/AMD/CMD)
前言 前端开发中,起初只要在script标签中嵌入几十上百行代码就能实现一些基本的交互效果,后来js得到重视,应用也广泛起来了, jQuery,Ajax,Node.Js,MVC,MVVM等的助力也使得 ...
随机推荐
- ansible系列2-常用命令
copyansible oldboy -m copy -a "src=/etc/hosts dest=/tmp/ mode=0600 owner=oldboy group=oldboy &q ...
- UOJ33 [UR #2] 树上GCD 【点分治】【容斥原理】【分块】
题目分析: 树上点对问题首先想到点分治.假设我们进行了点分治并递归地解决了子问题.现在我们合并问题. 我们需要找到所有经过当前重心$ c $的子树路径.第一种情况是LCA为当前重心$ c $.考虑以$ ...
- RedIsGood TopCoder - 9915(概率dp)
---恢复内容开始--- 论文题: 桌面上有 R 张红牌和 B 张黑牌,随机打乱顺序后放在桌面上,开始一张一张 地翻牌,翻到红牌得到 1 美元,黑牌则付出 1 美元.可以随时停止翻牌,在最优策略下平均 ...
- day22 ramdom 模块
import random #随机整数 random.randint(1,5) # 大于等于1且小于等于5之间的整数 random.randrange(1,10,2) # 大于等于1且小于10之间的奇 ...
- 自学Python5.1-面向对象与面向过程
自学Python之路 自学Python5.1-面向对象与面向过程 面向过程:根据业务逻辑从上到下写代码. 优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机.嵌入式开 ...
- SharePoint 2013 APP 开发示例 (一)List 读写
在这个示例里,我们将创建一个页面测试 SharePoint APP的权限.这个页面有二个按钮,一个从documents里读数据,一个往documents里写数据: 1. 打开Visual Studio ...
- 【codeforces 103E】 Buying Sets
http://codeforces.com/problemset/problem/103/E (题目链接) 题意 给出$n$个数,每个数与一个集合相关联.从其中选出最小的若干个数,选出的数的个数与这些 ...
- 【洛谷P2746】Network of Schools
题目大意:给定一个 N 个点,M 条边的有向图,第一问求至少从多少个点出发才能遍历整个有向图,第二问求至少在这个有向图的基础上加多少条边才能使得该无向图强连通. 题解:先进行 Tarjan 缩点,得到 ...
- strace命令,read,write
strace + 运行的程序,可以查看程序运行的过程中调用的系统函数 read.write函数常常被称为Unbuffered I/O.指的是无用户及缓冲区.但不保证不使用内核缓冲区.
- java.sql.SQLException: Prepared or callable statement has more than 2000 parameter markers及解决方案
1. 问题 最近在项目中修bug的时候,碰到这样一个错误: Caused by: java.sql.SQLException:Prepared or callable statement has mo ...