- Notifications
You must be signed in to change notification settings - Fork8
Implement
uupaa edited this pageOct 24, 2015 ·7 revisions
MyExample.js モジュールのソースコードは、lib/MyExample.js にあります。このファイルに必要な機能を実装していきます。
lib/MyExample.js は、function moduleExporter() { ... }
とfunction moduleClosure() { ... }
の2つの関数から構成されています。
詳細な説明はWebModule Idiom を参照してください。
lib/MyExample.js のfunction moduleExporter() { ... }
は、モジュールを global 空間に export し、node で require("MyExample") を出来るようにするための定形的なコードです。
(functionmoduleExporter(name,closure){"use strict";varentity=GLOBAL["WebModule"]["exports"](name,closure);if(typeofmodule!=="undefined"){module["exports"]=entity;}returnentity;})("MyExample",functionmoduleClosure(global){ : :returnMyExample;// return entity});
- モジュールを global 空間に export する処理は、webModule.exports 側で行われます
- 通常は moduleExporter の内部に手をいれる必要はありません
function moduleClosure() { ... }
がモジュールのクロージャに相当します。function MyExample() { ... }
はclass MyExamle
に相当します。
MyExample は MyExample.prototype.concat メソッドを持つシンプルなモジュールです。
最後から2行目でreturn MyExample;
を行い、モジュールの実体(moduleEntity) を返しています。
(functionmoduleExporter(name,closure){ : :})("MyExample",functionmoduleClosure(global){"use strict";// --- dependency modules ----------------------------------// --- define / local variables ----------------------------// --- class / interfaces ----------------------------------functionMyExample(value){//@arg String = "" - commentthis._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 ------------------------------------------functionMyExample_concat(a){//@arg String//@ret Stringreturnthis._value+a;}returnMyExample;// return entity});