Skip to content
uupaa edited this page May 31, 2015 · 7 revisions

MyExample.js モジュールのソースコードは、lib/MyExample.js にあります。このファイルに必要な機能を実装していきます。

Module has two code blocks

lib/MyExample.js は、function moduleExporter() { ... }function moduleClosure() { ... } の2つの関数から構成されています。

詳細な説明は WebModule Idiom を参照してください。

lib/MyExample.js のfunction moduleExporter() { ... }は、モジュールを global 空間に export し、node で require("MyExample") を出来るようにするための定形的なコードです。
moduleExporter の内側に手をいれる必要は通常ありません。

(function moduleExporter(moduleName, moduleClosure) { // http://git.io/WebModule
   "use strict";

    var moduleEntity = moduleClosure(GLOBAL);

    if (typeof webModule !== "undefined") {
        GLOBAL["webModule"]["exports"](moduleEntity, moduleName, moduleClosure);
    }
    if (typeof exports !== "undefined") {
        module["exports"] = moduleEntity;
    }
})("MyExample", function moduleClosure(global) {

    :
    :

    return MyExample; // return entity
});

function moduleClosure() { ... } がモジュールのクロージャに相当します。

function MyExample() { ... }class MyExamle に相当します。

MyExample は MyExample.prototype.concat メソッドを持つシンプルなモジュールです。

最後から2行目で return MyExample; を行い、モジュールの実体(moduleEntity) を返しています。

(function moduleExporter(moduleName, moduleClosure) { // http://git.io/WebModule


    :
    :

})("MyExample", function moduleClosure(global) {

"use strict";

// --- dependency modules ----------------------------------
// --- define / local variables ----------------------------
// --- class / interfaces ----------------------------------
function MyExample(value) { // @arg String = "" - comment
    this._value = value || "";
}

MyExample["repository"] = "https://github.com/uupaa/MyExample.js"; // GitHub repository URL.
MyExample["prototype"] = Object.create(MyExample, {
    "constructor":  { "value": MyExample          },  // new MyExample(value:String = ""):MyExample
    "concat":       { "value": MyExample_concat   },  // MyExample#concat(a:String):String
});

// --- implements ------------------------------------------
function MyExample_concat(a) { // @arg String
                               // @ret String
    return this._value + a;
}

return MyExample; // return entity

});
Clone this wiki locally