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');