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.

  1. define(factory);
  1. The define function accepts a single argument, the module factory.
  2. The factory may be a function or other valid values.
  3. If factory is a function, the first three parameters of the function, if specified, must be "require", "exports", and "module", in that order.
  4. If factory is not a function, then the module's exports are set to that object.

Module Context

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

  1. define(function(require, exports, module) {
  2.  
  3. // The module code goes here
  4.  
  5. });

The require Function

  1. require is a function

    1. require accepts a module identifier.
    2. require returns the exported API of the foreign module.
    3. If requested module cannot be returned, require should return null.
  2. require.async is a function

    1. require.async accepts a list of module identifiers and a optional callback function.
    2. The callback function receives module exports as function arguments, listed in the same order as the order in the first argument.
    3. 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

  1. module.uri

    The full resolved uri to the module.

  2. module.dependencies

    A list of module identifiers that required by the module.

  3. module.exports

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

Module Identifier

  1. A module identifier is and must be a literal string.
  2. Module identifiers may not have a filename extensions like .js.
  3. Module identifiers should be dash-joined string, such as foo-bar.
  4. Module identifiers can be a relative path, like ./foo and ../bar.

Sample Code

A typical sample

math.js

  1. define(function(require, exports, module) {
  2. exports.add = function() {
  3. var sum = 0, i = 0, args = arguments, l = args.length;
  4. while (i < l) {
  5. sum += args[i++];
  6. }
  7. return sum;
  8. };
  9. });

increment.js

  1. define(function(require, exports, module) {
  2. var add = require('math').add;
  3. exports.increment = function(val) {
  4. return add(val, 1);
  5. };
  6. });

program.js

  1. define(function(require, exports, module) {
  2. var inc = require('increment').increment;
  3. var a = 1;
  4. inc(a); // 2
  5.  
  6. module.id == "program";
  7. });

Wrapped modules with non-function factory

object-data.js

  1. define({
  2. foo: "bar"
  3. });

array-data.js

  1. define([
  2. 'foo',
  3. 'bar'
  4. ]);

string-data.js

  1. define('foo bar');

译文:

本规范阐述了如何编写模块,以便在基于浏览器的环境中进行互操作。本规范定义了模块系统必须提供的最小功能,以支持互操作模块。

  • 模块应该是单例。
  • 不应引入模块作用域范围内的新的自由变量。(模块作用域范围内不应引入新的自由变量)(其他模块不应引入模块作用域范围内的新的自由变量)
  • 执行必须是懒惰的。

模块定义

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

例如:

  1. define(factory);

define函数接受单个参数,即模块工厂。

工厂可能是一个函数或其他有效值。
如果factory是一个函数,函数的前三个参数(如果指定的话)必须是“require”,“exports”和“module”。
如果工厂不是一个函数(不是函数那必然就是对象或者基本类型了),那么模块的export属性应该设置为该对象(这里这个“对象”两个字,意思是“那个传入模块的单个参数,即模块工程,因为它不是函数,那么可能是js对象或者js基本类型)。

模块上下文

一个模块中有三个自由变量:require,exports 和 module。

例如:

  1. define(function(require, exports, module) {
  2.  
  3. // The module code goes here
  4.  
  5. });

require函数

1. ”require“是一个这样的函数:

  1. require 函数接收一个模块标识符(模块标识符也叫模块id)。
  2. require 函数返回外部模块的导出API(”导出API“是用来导出内容给外部模块使用的)。
  3. 如果无法返回请求的模块, require 函数将返回null。

2. ”require.async“ 是一个这样的函数:

  1. require.async 接收一个模块Id列表和一个可选的回调函数。
  2. 回调函数接收模块导出作为函数参数,按照与第一个参数中的顺序相同的顺序列出。
  3. 如果不能返回请求的模块,则回调应该相应地收到null。

exports对象

每个模块中都有个名叫"exports"的自由变量,这是一个模块可以在模块执行时添加模块API的对象。

module对象

1. module.uri:完整解析的模块URI(模块URI的全路径)。

2. module.dependencies:模块请求的标识符(模块id)列表。

3. module.exports:模块的导出API(”导出API“是”用来导出什么东西的API“)。 它与export对象相同。

模块标识符(模块id)

  1. 模块的标识符(模块id)必须是字面量字符串。
  2. 模块标识符(模块id)不能有类似  .js 的文件名扩展
  3. 模块标识符(模块id)应该是加前/后缀的字符串,比如:foo-bar。
  4. 模块标识符(模块id)可以是相对路径,例如:  ./foo 和 ../bar.。

示例代码

一个典型的例子:

math.js

  1. define(function(require, exports, module) {
  2. exports.add = function() {
  3. var sum = 0, i = 0, args = arguments, l = args.length;
  4. while (i < l) {
  5. sum += args[i++];
  6. }
  7. return sum;
  8. };
  9. });

increment.js

  1. define(function(require, exports, module) {
  2. var add = require('math').add;
  3. exports.increment = function(val) {
  4. return add(val, 1);
  5. };
  6. });

program.js

  1. define(function(require, exports, module) {
  2. var inc = require('increment').increment;
  3. var a = 1;
  4. inc(a); //
  5.  
  6. module.id == "program";
  7. });

使用非函数的工厂包装模块

object-data.js

  1. define({
  2. foo: "bar"
  3. });

array-data.js

  1. define([
  2. 'foo',
  3. 'bar'
  4. ]);

string-data.js

  1. define('foo bar');

公共模块定义/草案(Common Module Definition / draft - CMD草案)的更多相关文章

  1. 【SysML】模块定义图(BDD, Block Definition Diagram)

    一.引言 SysML中的模块定义图,英文为 “Block Definition Diagram”,简称BDD,是系统建模过程中最为常见的图之一,BDD是一种结构图,它主要对系统的结构组成以及组成元素间 ...

  2. CMD规范(通用模块定义规范)(翻译)

    最近在使用sea.js.大家知道sea.js遵循CMD规范.该规范的英文说明很简洁,我试着翻译了一下,旨在交流. Common Module Definition 通用模块定义规范 This spec ...

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

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

  4. CMD 模块定义规范

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

  5. CMD模块定义规范

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

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

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

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

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

  8. 理解JS中的模块规范(CommonJS,AMD,CMD)

    随着互联网的飞速发展,前端开发越来越复杂.本文将从实际项目中遇到的问题出发,讲述模块化能解决哪些问题,以及如何使用 Sea.js 进行前端的模块化开发. 恼人的命名冲突 我们从一个简单的习惯出发.我做 ...

  9. JS JavaScript模块化(ES Module/CommonJS/AMD/CMD)

    前言 前端开发中,起初只要在script标签中嵌入几十上百行代码就能实现一些基本的交互效果,后来js得到重视,应用也广泛起来了, jQuery,Ajax,Node.Js,MVC,MVVM等的助力也使得 ...

随机推荐

  1. Twitter数据挖掘:如何使用Python分析大数据 (3)

    让我们来拉取Twitter账号@NyTimes的最近20条微博. 我们可以创建变量来存放待拉取的微博数量(即count),以及待拉取的用户(即name).然后用这两个参数调用user_timeline ...

  2. 自学Python4.6-迭代器

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  3. LoadRunner回放乱码

    乱码的原因和服务器字符集编码相关,默认LR是gbk解码,如果服务器是utf8,那么需要改为utf8解码,这样中文就显示正常了. 通过查看源码,可以知道服务器的编码方式:<meta charset ...

  4. Azure HDInsight 上的 Spark 群集配合自定义的Python来分析网站日志

    一.前言:本文是个实践博客,演示如何结合使用自定义库和 HDInsight 上的 Spark 来分析日志数据. 我们使用的自定义库是一个名为 iislogparser.py的 Python 库. 每步 ...

  5. (转)Servlet的生命周期——初始化、运行、销毁全部过程

    背景:面试中很基础的一个问题,所以有必要好好整理一番. Servlet体系结构是建立在 Java 多线程机制上的,它的生命周期由 Web 容器负责. 当客户端第一次请求某个 Servlet 时,Ser ...

  6. R语言:随机抽样(sample函数)

    如果想从一堆数据集中随机抽出一个数,用sample函数就能实现,代码展示如下: forehead<-c(249,189,128,111,184,233,313,120,151,196,135,1 ...

  7. LVS+Keepalived实现MySQL从库读操作负载均衡配置

    转载于osyunwei 说明: 操作系统:CentOS 5.X 64位 MySQL主服务器:192.168.21.126 MySQL从服务器:192.168.21.127,192.168.21.128 ...

  8. HDFS集群PB级数据迁移方案-DistCp生产环境实操篇

    HDFS集群PB级数据迁移方案-DistCp生产环境实操篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 用了接近2个星期的时间,终于把公司的需要的大数据组建部署完毕了,当然,在部 ...

  9. python---网络之邮件发送

    在发送邮件之前需要先设置邮件开启了SMTP等功能,对于163还需要获取授权码 这里设置保存修改 这里获取授权码 完整封装发送邮件类 import smtplib from email.mime.tex ...

  10. python学习笔记9-单元测试unittest

    Python中有一个自带的单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一些校验返回的结果方法和一些用例执行前的初始化操作. 在说unittest之前,先说几个概念: TestC ...