CommonJS的wiki资料原文

Modules/AsynchronousDefinition

异步定义的模块(国内一般叫AMD模块定义)

STATUS: PROPOSAL

现状:提案

Implementations
实现:RequireJS、Nodules、Yabble、Dojo、Transporter、Teleport、partial、curl.js
[[Implementations/RequireJS|]],
[[Implementations/Nodules|]],
[[Implementations/Yabble
(partial)|]],
[[Implementations/Dojo|]],
[[Implementations/Transporter|]],
[[Implementations/Teleport|]],
[[Implementations/PINF (partial)|]],
[[Implementations/curl.js|]]

NOTE: This document is retained for historical purposes, but the evolution of this API is now tracked on the amdjs wiki. Please use the amdjs wiki as the definitive API source.

注意:本文件保留的目的只是为记录历史,但这个API的演变,现在已经由amdjs维基跟踪。请使用amdjs Wiki作为最终的API的源。

The Asynchronous Module Definition API specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded. This is particularly well suited for the browser environment where synchronous loading of modules incurs performance, usability, debugging, and cross-domain access problems. This specification used to be called Modules Transport/C, but this is specification is not primarily geared for transported existing CommonJS modules, but for defining modules (although it can be used as a transport).

异步模块定义API指定了定义模块的机制,使得模块和它的依赖关系可以异步加载。这特别适合于浏览器中那种同步加载模块时,会导致性能,可用性,调试和跨域访问问题的环境。本规范使用被称为模块的运输证,但这是不规范,主要是为运输存在的CommonJS模块,但定义模块(虽然它可以作为一个交通工具)。

Specification

define() function

The specification defines a single function "define" that is available as a free variable or a global variable. The signature of the function:

  1. define(id?, dependencies?, factory);

The first argument, id, specifies the id of the module being defined. This argument is optional, and if it not present, the module id should default to the id of the module that the loader was requesting for the given response script. When present, the module id MUST be an absolute id (relative ids are not allowed.

The second argument, dependencies, is an array of the dependencies that are required by the module that is being defined. The dependencies must be resolved prior to execution of the module factory function, and the resolved values should be passed as arguments to the factory function with argument positions corresponding to index in the dependencies array. The dependencies ids may be relative ids, and should be resolved relative the module being defined. This specification defines three special dependency names that have a distinct resolution. If the value of "require", "exports", or "module" appear in the dependency list, the argument should be resolved to the corresponding free variable as defined by the CommonJS modules specification. This argument is optional. If omitted, it should default to ["require", "exports", "module"]. However, if the factory function's arity (length property) is less than 3, than the loader may choose to only call the factory with the number of arguments corresponding to the function's arity or length.

The third argument, factory, is a function that should be executed to instantiate the module or an object. If the factory is a function it should only be executed once. If the factory argument is an object, that object should be assigned as the exported value of the module.

If the factory function returns a value (an object, function, or any value that coerces to true), and then that value should be assigned as the exported value for the module.

If both the first argument (module id) and the second argument (dependencies) are omitted, the module loader MAY choose to scan the factory function for dependencies in the form of require statements (literally in the form of require("module-string")). The first argument must literally be named require for this to work. In some situations module loaders may choose not to scan for dependencies due to code size limitations or lack of toString support on functions (Opera Mobile is known to lack toString support for functions). If either the first or second argument is present, the module loader SHOULD NOT scan for dependencies within the factory function.

define.amd property

To allow a clear indicator that a global define function (as needed for script src browser loading) conforms to the AMD API, any global define function SHOULD have a property called "amd" whose value is an object. This helps avoid conflict with any other existing JavaScript code that could have defined a define() function that does not conform to the AMD API.

The properties inside the define.amd object are not specified at this time. It can be used by implementers who want to inform what other capabilities beyond the basic API that the implementation supports.

Existence of the define.amd property with an object value indicates conformance with this API. If there is another version of the API, it will likely define another property, like define.amd2, to indicate implementations that conform to that version of the API.

An example of how it may be defined for an implementation that allows loading more than one version of a module in an environment:

  1. define.amd = {
  2. multiversion: true
  3. };

The minimum definition:

  1. define.amd = {};

Transporting more than one module at a time

Multiple define calls can be made within a single script. The order of the define calls SHOULD NOT be significant. Earlier module definitions may specify dependencies that are defined later in the same script. It is the responsibility of the module loader to defer loading unresolved dependencies until the entire script is loaded to prevent unnecessary requests.

Examples

Using require and exports

Sets up the module with ID of "alpha", that uses require, exports and the module with ID of "beta":

  1. define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
  2. exports.verb = function() {
  3. return beta.verb();
  4. //Or:
  5. return require("beta").verb();
  6. }
  7. });

An anonymous module could be defined (module id derived from filename) that returns an object literal:

  1. define(["alpha"], function (alpha) {
  2. return {
  3. verb: function(){
  4. return alpha.verb() + 2;
  5. }
  6. };
  7. });

A dependency-free module can define a direct object literal:

  1. define({
  2. add: function(x, y){
  3. return x + y;
  4. }
  5. });

Global Variables

This specification reserves the global variable "define" for use in implementing this specification, the package metadata asynchronous definition API and is reserved for other future CommonJS APIs. Module loaders SHOULD not add additional methods or properties to this function.

This specification reserves the global variable "require" for use by module loaders. Module loaders are free to use this global variable as they see fit. They may use the variable and add any properties or functions to it as desired for module loader specific functionality. They can also choose not to use "require" as well.

Usage notes

It is recommended that define calls be in the literal form of 'define(...)' in order to work properly with static analysis tools (like build tools).

This specification has used require.def() as the entry point in the past. Module loaders may choose to alias define() to require.def() to provide backwards compatibility (this is within the module loader's reserved namespace, so this is the prerogative of the module loader).

翻译:CommonJS的wiki的更多相关文章

  1. Flume性能测试报告(翻译Flume官方wiki报告)

    因使用flume的时候总是会对其性能有所调研,网上找的要么就是自测的这里找到一份官方wiki的测试报告供大家参考 https://cwiki.apache.org/confluence/display ...

  2. hive 创建/删除/截断 表(翻译自Hive wiki)

    这里罗列常用操作,更多参考 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL- ...

  3. CommonJS Promises/A规范

    本文来自四火哥的翻译 CommonJS是一组javascript编程规范,而promise是其中之一. 简而言之,promises是一种令代码的异步行为变得更加优雅的软件抽象.在基本的定义中,代码可能 ...

  4. commonJS 浏览器实现

    commonjs http://www.commonjs.org/ CommonJS JavaScript is a powerful object oriented language with so ...

  5. 该如何理解AMD ,CMD,CommonJS规范--javascript模块化加载学习总结

    是一篇关于javascript模块化AMD,CMD,CommonJS的学习总结,作为记录也给同样对三种方式有疑问的童鞋们,有不对或者偏差之处,望各位大神指出,不胜感激. 本篇默认读者大概知道requi ...

  6. CommonJS初识

    CommonJS是什么 CommonJS是一个标准库,或者说是一组规范.因为官方并没有给出真正的代码实现,而是给出一些代码组织规范.常用模块的api.包(Package)的命名规范和具体格式. Com ...

  7. CommonJS 规范

    CommonJS 是以在浏览器环境之外构建 JavaScript 生态系统为目标而产生的项目,比如在服务器和桌面环境中. 这个项目最开始是由 Mozilla 的工程师 Kevin Dangoor 在2 ...

  8. JS模块规范:AMD,CMD,CommonJS

    浅析JS模块规范 随着JS模块化编程的发展,处理模块之间的依赖关系成为了维护的关键. AMD,CMD,CommonJS是目前最常用的三种模块化书写规范. CommonJS CommonJS规范是诞生比 ...

  9. requireJS 从概念到实战

    requireJS 可以很轻易的将一个项目中的JavaScript代码分割成若干个模块(module).并且requireJS推荐一个模块就是一个文件,所以,你将获得一些零碎的具有互相依赖关系的JS文 ...

随机推荐

  1. 【设计模式】—— 原型模式Prototype

    前言:[模式总览]——————————by xingoo 模式意图 由于有些时候,需要在运行时指定对象时哪个类的实例,此时用工厂模式就有些力不从心了.通过原型模式就可以通过拷贝函数clone一个原有的 ...

  2. .net MVC4 来一个简单的分页代码

    action VodeEntities db = new NewVode.Models.VodeEntities(); ; ) { var sql = "select * ,row_numb ...

  3. 【转载】JSP 获取真实IP地址的代码

    JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.   但是在通过了 Apache,Squid ...

  4. P1896 [SCOI2005]互不侵犯

    题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 注:数据有加强(2018/4/25) ...

  5. WampServer3.0允许局域网访问配置教程

    wamp server 下的Apache默认设置是不允许外网访问和局域网访问的,而直接搜索 wamp局域网访问配置,出现都是一些旧版本的wamp的配置信息,最新版本3.0.X需要使用以下配置方法: 配 ...

  6. WebSphere概要文件管理工具

    manageprofiles:概要管理命令工具 3.1 模板 位于 <was_home>/profileTemplates.每个模板都由一组提供概要初始设置的文件和概要创建后将采取的操作列 ...

  7. 【纪中集训2019.3.23】IOer

    题目 描述 你要在\(m\)天内,刷\(n\)道题,每天可以刷的题的数目不限: 第\(i\)天可以刷的题目的种类是\(ui+v\): 两种刷题的方案不同当且仅当某天刷题的数量不同或者依次刷题的种类不同 ...

  8. Struts2的配置文件中, <package>的作用,<action><result>重名?

    问:Struts2的配置文件中, <package>的作用是什么? 答:防止action重名啊,例如前台和后台,总会有很多地方起名重复的! 问:可是访问的时候,不也是访问action吗,能 ...

  9. 2018-2019 ACM-ICPC ECfinal I. Misunderstood … Missing

    题目链接 首先有两个个属性值:\(A,D\),其中\(A\)表示目前攻击力,\(D\)表示每回合攻击的增量. 现在一共有\(n\)个回合,每一回合\(i\),可以有以下三种操作: 1.进行攻击,造成\ ...

  10. 【Asp.net入门09】第一个ASP.NET 应用程序-处理窗体(1)

    我们创建了一个HTML窗体,可以通过它显示受邀参加晚会的嘉宾,但是,当嘉宾单击Submit RSVP按钮时,同一个页面会反复多次显示.为了解决此问题,需要实现一段代码,用于在将窗体数据发布到服务器时执 ...