2011-06-28 5 views
6

私はこの記事http://www.klauskomenda.com/code/javascript-programming-patterns/#revealingを読んでいて、プライベートプロパティをオーバーライドするためのパラメータを渡すことができるのだろうかと思っていました。モジュールパターンにパラメータを渡して、JavaScriptのデフォルト値[プライベートプロパティ]を上書きする方法はありますか?

// revealing module pattern 
var anchorChange4 = function() { 

    // this will be a private property 
    var config = { 
     colors: [ "#F63", "#CC0", "#CFF" ] 
    } 

    // this will be a public method 
    var init = function() { 
     var self = this; // assign reference to current object to "self" 

     // get all links on the page 
     var anchors = document.getElementsByTagName("a"); 
     var size = anchors.length; 

     for (var i = 0; i < size; i++) { 
      anchors[i].color = config.colors[i]; 

      anchors[i].onclick = function() { 
       self.changeColor(this, this.color); // this is bound to the anchor object 
       return false; 
      }; 
     } 
    } 

    // this will be a public method 
    var changeColor = function (linkObj, newColor) { 
     linkObj.style.backgroundColor = newColor; 
    } 

    return { 
     // declare which properties and methods are supposed to be public 
     init: init, 
     changeColor: changeColor 
    } 
}(); 

anchorChange4.init(); 

さまざまな色をパラメータとして渡すなど、配列の色の値を変更しようとしています。私は何か意味があると思う。

+0

あなたが '' config'をパラメータを受け入れて、この1とプライベート1を拡張init'行うことができます。 –

+0

@felixあなたはinit関数にパラメータを渡し、それらを使って設定を変更することを意味しますか? – manraj82

+0

@ manraj82:基本的にはい。 –

答えて

6

あなたはinitは、設定パラメータを受け入れ、このいずれかでプライベートコンフィギュレーションを拡張することができます:

var init = function (options) { 
    // copy properties of `options` to `config`. Will overwrite existing ones. 
    for(var prop in options) { 
     if(options.hasOwnProperty(prop)){ 
      config[prop] = options[prop]; 
     } 
    } 
    //... 
} 

次にあなたがinitにオブジェクトを渡すことができます。

anchorChange4.init({ 
    colors: ['#FFF', '#000'] 
}); 
+0

あなたの答えをありがとう、しかし、私は非常にif(options.hasOwnProperty(小道具))ビットを取得しませんでした、あなたはそれを説明するablをしていただけますか?そして私もこの記事を読んでいました。http://www.engfers.com/code/javascript-module-pattern/、彼らはどちらも匿名のオブジェクトを返していますが、宣言されている方法とは異なります。私に教えてください違い? – manraj82

+1

@ manraj82: 'hasOwnProperty'に関しては、[documentation](https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)を見てください。基本的に継承されたプロパティをコピーしないようにするのは安全です。違いは、あなたのコードでは、あなたは "モジュール"のインスタンスを1つしか持っていませんが、他のコードでは複数のインスタンスを持つことができます。あなたのコードを 'var anchorChange4 = function(){...};'(最後に括弧を入れずに)に変更すると、同じことができます。 –

0

は、プラグインのために似た何かをしました私は、少し異なる方法で、オブジェクトの渡された引数をチェックしています:

(function() { 

    'use strict'; 


    var MyPlugin = function(){ 

     var options = null; 

     var defaults = { 
      className: 'fade-and-drop', 
      closeButton: true 
     } 


     var init = function() { 

      // First Extend Default Options 
      if (arguments[0] && typeof arguments[0] === "object") { 
       options = _extendDefaults(defaults, arguments[0]); 
      } 

      _somePrivate(); 
     } 

     var _somePrivate = function(){ 
      //access option as option.parameter 
     } 

     // Extend defaults with user options 
     var _extendDefaults = function(source, properties) { 
      var property; 
      for (property in properties) { 
       if (properties.hasOwnProperty(property)) { 
       source[property] = properties[property]; 
       } 
      } 
      return source; // to set options var 
      } 


     return { 
      init:init, 
     } 

    }(); 

    MyPlugin.init({ 
     selector:'input__field--select', 
     maxWidth: 750 
    }); 

}()); 
1

あなたは、単にあなたの帰りのオブジェクトに次のようにして、これを行うことができます: -

return { 
     Init: function(params) { init(params); }, 
     Start: start, 
     Stop: stop, 
     Pause: pause 
    } 
関連する問題