Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass parameters to a module pattern to override default values [private properties] in JavaScript?

Tags:

javascript

I was reading this article http://www.klauskomenda.com/code/javascript-programming-patterns/#revealing and was wondering if I can pass parameters to override the private properties.

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

I'm trying to change the values of the Array colors, like passing different colors as parameters. I hope I'm making some sense.

like image 929
manraj82 Avatar asked Jun 28 '11 09:06

manraj82


People also ask

Can I use default parameters JavaScript?

In JavaScript, function parameters default to undefined . However, it's often useful to set a different default value. This is where default parameters can help. In the past, the general strategy for setting defaults was to test parameter values in the function body and assign a value if they are undefined .

Can we use default parameter for first parameter in function?

You still need to provide the first parameter regardless of its default value.

What is the module pattern in JavaScript?

The Module Pattern is one of the important patterns in JavaScript. It is a commonly used Design Pattern which is used to wrap a set of variables and functions together in a single scope. It is used to define objects and specify the variables and the functions that can be accessed from outside the scope of the function.

Do I always have to add parameters to every function in JavaScript?

Nothing will happen- meaning you won't get an error or a warning as passing the parameters in javascript is optional. All the parameters that weren't "supplied" will have the undefined value.


2 Answers

You can make init accept a configuration parameter and extend the private configuration with this one:

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];
        }
    }
    //...
}

Then you can pass an object to init:

anchorChange4.init({
    colors: ['#FFF', '#000']
});
like image 62
Felix Kling Avatar answered Oct 03 '22 11:10

Felix Kling


You can do this simply by doing the following in your return object:-

return {
        Init: function(params) { init(params); },
        Start: start,
        Stop: stop,
        Pause: pause
    }
like image 34
Stephen Garside Avatar answered Oct 03 '22 11:10

Stephen Garside