fbpx
维基百科

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
  • Promises/B
  • Promises/C
  • 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. ^ . [2019-05-17]. (原始内容存档于2012-11-21). 
  11. ^ . [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. ^ . [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. ^ . [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. ^ . [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). 

commonjs, 是一个项目, 其目标是为javascript在网页浏览器之外建立模块约定, 创建这个项目的主要原因是当时缺乏普遍可接受形式的javascript脚本模块单元, 模块在与运行javascript脚本的常规网页浏览器所提供的不同的环境下可以重复使用, 目录, 历史, 规定, 当前, 提议, 模块, 样例代码, 实现, 参见, 引用, 外部链接历史, 编辑这个项目由mozilla工程师kevin, dangoor于2009年1月发起, 最初名为serverjs, 在2009年8月, 这个项目被改名为,. CommonJS是一个项目 其目标是为JavaScript在网页浏览器之外建立模块约定 创建这个项目的主要原因是当时缺乏普遍可接受形式的JavaScript脚本模块单元 模块在与运行JavaScript脚本的常规网页浏览器所提供的不同的环境下可以重复使用 目录 1 历史 2 规定 2 1 当前 2 2 提议 3 模块 3 1 样例代码 4 实现 5 参见 6 引用 7 外部链接历史 编辑这个项目由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 Promises B Promises C 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 jsexports add function var sum 0 i 0 args arguments l args length while i lt l sum args i return sum increment jsvar add require math add exports increment function val return add val 1 program jsvar 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 参见 编辑服务器端JavaScript实现列表 英语 List of server side JavaScript implementations 文档对象模型 DOM 网页浏览器客户端应用程序接口 API 通常以JavaScript获得 JSGI 英语 JSGI 引用 编辑 What Server Side JavaScript needs Blue Sky On Mars 2019 05 17 原始内容存档于2017 12 24 CommonJS JavaScript Standard Library 2019 05 17 原始内容存档于2010 05 21 ProposalProcess CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 09 06 CommonJS the First Year Blue Sky On Mars 2019 05 17 原始内容存档于2017 09 06 Schlueter Isaac Z Forget CommonJS It s dead We are server side JavaScript GitHub 25 Mar 2013 2019 05 17 原始内容存档于2015 05 08 CommonJS Spec Wiki 2019 05 17 原始内容存档于2019 05 17 Modules 1 0 页面存档备份 存于互联网档案馆 Modules 1 1 1 页面存档备份 存于互联网档案馆 Server side JavaScript development and hosting Akshell 2020 09 25 原始内容存档于2018 04 08 olegp common node GitHub 2019 05 17 原始内容存档于2012 11 21 GitHub 2019 05 17 原始内容存档于2018 09 30 DrBenton CommonJSForPHP GitHub 2019 05 17 原始内容存档于2019 09 24 Implementations CouchDB CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 04 26 Flusspferd CommonJS平台 Javascript的C绑定 2019 05 17 原始内容存档于2013 01 28 Implementations GPSEE CommonJS规范Wiki 2019 05 17 原始内容存档于2017 04 26 Implementations Smart CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 04 26 Homepage JSBuild 2019 05 17 原始内容存档于2011 01 04 MongoDB 2019 05 17 原始内容存档于2014 01 22 Implementations Narwhal CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 04 26 Implementations node js CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 08 30 Implementations Persevere CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 04 25 pinf loader js GitHub 2019 05 17 原始内容存档于2014 01 07 Implementations RingoJS CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 04 25 SilkJS WWW Site 2019 05 17 原始内容存档于2017 09 12 Implementations SproutCore CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 04 25 Implementations TeaJS CommonJS Spec Wiki 2019 05 17 原始内容存档于2017 04 26 Wakanda 2019 05 17 原始内容存档于2012 06 04 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 取自 https zh wikipedia org w index php title CommonJS amp oldid 73481243, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。