私はこの記事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();
さまざまな色をパラメータとして渡すなど、配列の色の値を変更しようとしています。私は何か意味があると思う。
あなたが '' config'をパラメータを受け入れて、この1とプライベート1を拡張init'行うことができます。 –
@felixあなたはinit関数にパラメータを渡し、それらを使って設定を変更することを意味しますか? – manraj82
@ manraj82:基本的にはい。 –