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

外部連結