如何将参数传递给模块模式以覆盖JavaScript中的默认值[私有属性]?

如何将参数传递给模块模式以覆盖JavaScript中的默认值[私有属性]?,javascript,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 =

我正在阅读这篇文章,想知道是否可以传递参数来覆盖私有属性

// 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();
//显示模块模式
var anchorChange4=函数(){
//这将是私人财产
变量配置={
颜色:[“F63”、“CC0”、“CFF”]
}
//这将是一种公共方法
var init=函数(){
var self=this;//将对当前对象的引用分配给“self”
//获取页面上的所有链接
var archors=document.getElementsByTagName(“a”);
变量大小=锚点长度;
对于(变量i=0;i

我试图改变数组颜色的值,比如传递不同的颜色作为参数。我希望我讲得有点道理。

您可以让
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']
});

对我正在使用的插件执行了类似的操作,使用了稍微不同的方法并检查对象的传递参数:

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

}());

只需在返回对象中执行以下操作即可:-

return {
        Init: function(params) { init(params); },
        Start: start,
        Stop: stop,
        Pause: pause
    }

您可以让
init
接受一个
config
参数,并用这个参数扩展私有参数。@felix您的意思是将参数传递给init函数并使用它们更改配置?@felix您能告诉我如何用传递的参数扩展私有值吗?您不能覆盖私有属性,这就是为什么它们是私人的。您应该使用公共方法设置属性,如
var setColors=function(colors){this.config.colors=colors;}
并使用
obj.setColors([“#F63”、“#CC0”、“#CFF”])
@manraj82:请看一下我的答案。谢谢你的回答,但我没有完全理解if(options.hasOwnProperty(prop))位,请你解释一下好吗?我也在读这篇文章,我知道它们都返回匿名对象,但它们的声明方式不同。你能告诉我区别吗?@manraj82:关于
hasOwnProperty
,请看一下。不复制继承的属性基本上是一种安全措施。不同之处在于,在代码中,您只有一个“模块”实例,而在其他代码中,您可以有多个。如果您将代码更改为
var-anchorChange4=function(){…}(末尾没有括号)您也可以这样做。