Skip to content
uupaa edited this page Oct 24, 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") を出来るようにするための定形的なコードです。

(function moduleExporter(name, closure) {
"use strict";

var entity = GLOBAL["WebModule"]["exports"](name, closure);

if (typeof module !== "undefined") {
    module["exports"] = entity;
}
return entity;

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

    :
    :

    return MyExample; // return entity
});
  • モジュールを global 空間に export する処理は、webModule.exports 側で行われます
  • 通常は moduleExporter の内部に手をいれる必要はありません
  • function moduleClosure() { ... } がモジュールのクロージャに相当します。
  • function MyExample() { ... }class MyExamle に相当します。

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

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

(function moduleExporter(name, closure) {

    :
    :

})("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

});