CommonJS

本页使用了标题或全文手工转换
维基百科,自由的百科全书

CommonJS是一个项目,其目标是为JavaScript网页浏览器之外建立模块约定。创建这个项目的主要原因是当时缺乏普遍可接受形式的JavaScript脚本模块单元,模块在与运行JavaScript脚本的常规网页浏览器所提供的不同的环境下可以重复使用。

历史[编辑]

这个项目由Mozilla工程师Kevin Dangoor于2009年1月发起,最初名为ServerJS[1]。在2009年8月,这个项目被改名为“CommonJS”来展示其API的广泛的应用性[2]。有关规定在一个开放进程中被建立和认可,一个规定只有在已经被多个实现完成之后才被认为是最终的[3]。 CommonJS不隶属于致力于ECMAScriptEcma国际的工作组 TC39,但是TC39的一些成员参与了这个项目[4]

在2013年5月,Node.js包管理器npm的作者Isaac Z. Schlueter,宣布Node.js已经废弃了CommonJS,Node.js核心开发者应避免使用它[5]

规定[编辑]

规定列表包括[6]

当前[编辑]

  • Modules/1.0 (被Modules/1.1取代)
  • Modules/1.1
  • Modules/1.1.1
  • Packages/1.0
  • System/1.0

提议[编辑]

  • Binary/B
  • Binary/F
  • Console
  • Encodings/A
  • Filesystem/A
  • Filesystem/A/0
  • Modules/Async/A
  • Modules/Transport/B
  • Packages/1.1
  • Packages/Mappings
  • Unit Testing/1.0

模块[编辑]

require是一个函数,require函数接受一个模块标识符,require返回外部模块的导出的API。如果要求的模块不能被返回则require必须throw一个错误。在模块内,有一个自由变量require,它满足上述定义。在模块内,有一个自由变量叫做exports,它是一个对象,模块在执行时可以向其增加模块的API。模块必须使用exports对象作为唯一的导出方式。[7]

在模块中,必须有一个自由变量module,它是一个对象。module对象必须有一个id属性,它是这个模块的顶层id。id属性必须是这样的:require(module.id)会从源出module.id的那个模块返回exports对象。(就是说module.id可以被传递到另一个模块,而且在要求它时必须返回最初的模块)。[8]

样例代码[编辑]

math.js
exports.add = function() {
    var sum = 0, i = 0, args = arguments, l = args.length;
    while (i < l) {
        sum += args[i++];
    }
    return sum;
};
increment.js
var add = require('math').add;
exports.increment = function(val) {
    return add(val, 1);
};
program.js
var inc = require('increment').increment;
var a = 1;
inc(a); // 2

module.id == "program";

实现[编辑]

参见[编辑]

引用[编辑]

  1. ^ What Server Side JavaScript needs - Blue Sky On Mars. [2019-05-17]. (原始内容存档于2017-12-24). 
  2. ^ CommonJS: JavaScript Standard Library. [2019-05-17]. (原始内容存档于2010-05-21). 
  3. ^ ProposalProcess - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-09-06). 
  4. ^ CommonJS: the First Year - Blue Sky On Mars. [2019-05-17]. (原始内容存档于2017-09-06). 
  5. ^ Schlueter, Isaac Z. Forget CommonJS. It's dead. **We are server side JavaScript.**. GitHub. 25 Mar 2013 [2019-05-17]. (原始内容存档于2015-05-08). 
  6. ^ CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2019-05-17). 
  7. ^ Modules/1.0页面存档备份,存于互联网档案馆).
  8. ^ Modules/1.1.1页面存档备份,存于互联网档案馆).
  9. ^ Server-side JavaScript development and hosting - Akshell. [2020-09-25]. (原始内容存档于2018-04-08). 
  10. ^ olegp/common-node @ GitHub. [2019-05-17]. (原始内容存档于2012-11-21). 
  11. ^ - GitHub. [2019-05-17]. (原始内容存档于2018-09-30). 
  12. ^ DrBenton/CommonJSForPHP - GitHub. [2019-05-17]. (原始内容存档于2019-09-24). 
  13. ^ Implementations/CouchDB - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-04-26). 
  14. ^ Flusspferd - CommonJS平台 | Javascript的C绑定. [2019-05-17]. (原始内容存档于2013-01-28). 
  15. ^ Implementations/GPSEE - CommonJS规范Wiki. [2019-05-17]. (原始内容存档于2017-04-26). 
  16. ^ Implementations/Smart - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-04-26). 
  17. ^ Homepage - JSBuild. [2019-05-17]. (原始内容存档于2011-01-04). 
  18. ^ MongoDB. [2019-05-17]. (原始内容存档于2014-01-22). 
  19. ^ Implementations/Narwhal - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-04-26). 
  20. ^ Implementations/node.js - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-08-30). 
  21. ^ Implementations/Persevere - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-04-25). 
  22. ^ pinf/loader-js - GitHub. [2019-05-17]. (原始内容存档于2014-01-07). 
  23. ^ Implementations/RingoJS - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-04-25). 
  24. ^ SilkJS WWW Site. [2019-05-17]. (原始内容存档于2017-09-12). 
  25. ^ Implementations/SproutCore - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-04-25). 
  26. ^ Implementations/TeaJS - CommonJS Spec Wiki. [2019-05-17]. (原始内容存档于2017-04-26). 
  27. ^ Wakanda. [2019-05-17]. (原始内容存档于2012-06-04). 
  28. ^ xuljet - XUL JavaScript Enhanced Toolkit - Google Project Hosting. [2019-05-17]. (原始内容存档于2011-02-12). 

外部链接[编辑]