- 追加された行はこの色です。
- 削除された行はこの色です。
* モジュールの作り方 [#p895323a]
* モジュールパターン [#p895323a]
モジュールパターンとはシングルトンパターンの一種で、オブジェクトのプロパティをパブリック・プライベートにする。Douglas Crockford氏によって提唱されたらしい。
var module = function() {
var current = null;
var config = {
'num1' : 1,
'num2' : 2
var v1 = null;
var v2 = 10;
function init(v) {
v1 = v;
};
function init() {
function func() {
return _func();
};
function show() {
current = 1;
function _func() {
return v1 * v2;
};
function hide() {
show();
};
function value() {
return v1;
}
return {
init : init,
show : show,
current : current
init : init,
func : func,
value : value
};
}();
- 無名関数によって変数や関数を局所化する。
- オブジェクトリテラルを返すモジュールパターンによりグローバルからアクセス出来るようにする。
- オブジェクトリテラルから内部の変数や関数を参照するようにする事で、内部の変数や関数の書式を一貫したものにする。
** 参考 [#ra57d02f]
- JavaScript "Best Practices"
- http://www.slideshare.net/cheilmann/javascript-best-practices-1041724