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

外部連結[編輯]