AMD that
Asynchronous Module Definition
is, the Chinese name is the meaning of "async module definition". It is a specification for modular development in the browser-side, and the server-
CommonJS。
side specification is
模块将被异步加载,模块加载不影响后面语句的运行。所有依赖某些模块的语句均放置在回调函数中。
AMD
is the
RequireJS
normalized output defined for the module in the promotion process. define () function
The AMD specification defines only one function define
, which is a global variable. The description of the function is:
define(id?, dependencies?, factory);
id:指定义中模块的名字,可选;如果没有提供该参数,模块的名字应该默认为模块加载器请求的指定脚本的名字。如果提供了该参数,模块名必须是“顶级”的和绝对的(不允许相对名字)
。
format of module name
模块名是用正斜杠分割的有意义单词的字符串单词须为驼峰形式,或者".",".."模块名不允许文件扩展名的形式,如“.js”模块名可以为 "相对的" 或 "顶级的"。如果首字符为“.”或“..”则为相对的模块名顶级的模块名从根命名空间的概念模块解析相对的模块名从 "require" 书写和调用的模块解析
模块名用来唯一标识定义中模块,它们同样在依赖性数组中使用:
依赖dependencies:是一个当前模块依赖的,已被模块定义的模块标识的数组字面量。依赖参数是可选的,如果忽略此参数,它应该默认为["require", "exports", "module"]。然而,如果工厂方法的长度属性小于3,加载器会选择以函数的长度属性指定的参数个数调用工厂方法。
工厂方法factory:模块初始化要执行的函数或对象。依赖模块必须根据模块的工厂方法优先级执行,并且执行的结果应该按照依赖数组中的位置顺序以参数的形式传入(定义中模块的)工厂方法中。如果为函数,它应该只被执行一次。如果是对象,此对象应该为模块的输出值。
Example
创建一个名为"alpha"的模块,使用了require,exports,和名为"beta"的模块:
Define ("Alpha", ["require", "exports", "beta"], function (require, exports, beta) { exports.verb=function () { return Beta.verb (); Or:returnrequire ("beta"). verb (); } });
an anonymous module that returns an object:
define (["alpha"], function (Alpha) { return { verb:function () { return alpha.verb () +2; } }; });
a module without dependencies can define objects directly:
Define ({ add:function (x, y) { return x + y; } });
A module definition that uses a simple COMMONJS transformation:
Define (function (Require, exports, module) { var a =require (' a '), b =require (' B '); Exports.action=function () {}; });
require API
Description: Https://github.com/amdjs/amdjs-api/wiki/require
AMD
Specification Chinese version: https://github.com/amdjs/amdjs-api/wiki/amd-(%e4%b8%ad%e6%96%87%e7%89%88)
Currently, the implementation of AMD's library is available RequireJS 、curl 、Dojo 、Nodules
.
AMD Module Definition Specification