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.git
A module is defined with define
keyword, which is a function.github
define(factory);
define
function accepts a single argument, the module factory.factory
may be a function or other valid values.factory
is a function, the first three parameters of the function, if specified, must be "require", "exports", and "module", in that order.factory
is not a function, then the module's exports are set to that object.In a module, there are three free variables: require
, exports
and module
.浏览器
define(function(require, exports, module) { // The module code goes here });
require
Functionrequire
is a functionapp
require
accepts a module identifier.require
returns the exported API of the foreign module.require
should return null.require.async
is a functionasync
require.async
accepts a list of module identifiers and a optional callback function.exports
ObjectIn a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.ide
module
Objectmodule.uri
函数
The full resolved uri to the module.ui
module.dependencies
this
A list of module identifiers that required by the module.spa
module.exports
The exported API of the module. It is the same as exports
object.
.js
.foo-bar
../foo
and ../bar
.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
});
1. ”require“是一个这样的函数:
require
函数接收一个模块标识符(模块标识符也叫模块id)。require
函数返回外部模块的导出API(”导出API“是用来导出内容给外部模块使用的)。require
函数将返回null。2. ”require.async“ 是一个这样的函数:
require.async
接收一个模块Id列表和一个可选的回调函数。每一个模块中都有个名叫"exports"的自由变量,这是一个模块能够在模块执行时添加模块API的对象。
1. module.uri:完整解析的模块URI(模块URI的全路径)。
2. module.dependencies:模块请求的标识符(模块id)列表。
3. module.exports:模块的导出API(”导出API“是”用来导出什么东西的API“)。 它与export对象相同。
模块标识符(模块id)不能有相似 .js 的文件名扩展
。./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); // 2 module.id == "program"; });
使用非函数的工厂包装模块
object-data.js
define({ foo: "bar" });
array-data.js
define([ 'foo', 'bar' ]);
string-data.js
define('foo bar');