CommonJS

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

历史

这个项目由Mozilla工程师Kevin Dangoor于2009年1月发起,最初名为ServerJS[1]。在2009年8月,这个项目被改名为“CommonJS”来展示其API的广泛的应用性[2]。有关规定在一个开放进程中被建立和认可,一个规定只有在已经被多个实现完成之后才被认为是最终的[3]。 CommonJS不隶属于致力于ECMAScript的Ecma国际的工作组 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";

实现

  • Akshell[9]
  • Common Node[10]
  • CommonJS Compiler - 命令行工具使Common JS模块适用于浏览器内使用[11]
  • CommonJS for PHP - 针对PHP 5.3+的轻量级CommonJS实现[12]
  • CouchDB[13]
  • Flusspferd[14]
  • GPSEE[15]
  • Jetpack
  • Joyent 智能平台[16]
  • JSBuild[17]
  • MongoDB[18]
  • Narwhal (JavaScript平台)[19]
  • Node.js[20]
  • Persevere[21]
  • PINF JavaScript装载器[22]
  • RingoJS[23]
  • SilkJS[24]
  • SproutCore英语SproutCore[25]
  • TeaJS[26]
  • Wakanda英语Wakanda (software)[27]
  • XULJet英语XULJet[28]
  • QUnit英语QUnit

参见

引用

  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). 

外部链接

  • 官方网站
  • CommonJS effort sets JavaScript on path for world domination. Ars Technica. Dec 2009 [2019-05-17]. (原始内容存档于2011-08-22). 
语言
引擎列表
引擎比较
框架
客户端
  • Ample SDK英语Ample SDK
  • Chaplin.js英语Chaplin.js
  • Dojo
  • Echo
  • Extjs
  • Google網頁工具包
  • JQuery
  • Lively Kernel英语Lively Kernel
  • midori英语Midori JavaScript Framework
  • MochiKit英语MochiKit
  • MooTools英语MooTools
  • Prototype
  • Pyjs英语Pyjs
  • qooxdoo英语qooxdoo
  • Rialto英语Rialto Toolkit
  • Rico
  • script.aculo.us英语script.aculo.us
  • SmartClient英语SmartClient
  • SproutCore英语SproutCore
  • Spry英语Spry framework
  • Wakanda框架英语Wakanda (software)
  • 雅虎UI库
服务器
  • AppJet英语AppJet
  • Jaxer英语Jaxer#Aptana Jaxer
  • Node.js
  • Deno
  • WakandaDB英语Wakanda (software)
多種實作
  • Cappuccino英语Cappuccino (application development framework)
    • Objective-J英语Objective-J
  • PureMVC英语PureMVC
函式庫
  • Backbone.js
  • SWFObject英语SWFObject
  • SWFAddress英语SWFAddress
  • Lodash
人物
其他
  • DHTML
  • Ecma国际
  • JSDoc英语JSDoc
  • JSGI英语JSGI
  • JSHint
  • JSLint
  • JSON
  • JSSS英语JavaScript Style Sheets
  • Sputnik英语Sputnik (JavaScript conformance test)
  • SunSpider英语Browser speed test#SunSpider
  • 异步模块定义英语Asynchronous module definition
  • CommonJS
列表级条目列表
JavaScript库列表
Ajax框架英语list of Ajax frameworks#JavaScript
列表级条目比较
JavaScript框架英语Comparison of JavaScript frameworks
服务器端JavaScript英语comparison of server-side JavaScript solutions
软件
框架
编程语言
云平台