Is there a way to pass a parameter to a module in the same manner as a constructor?
The answers I have been able to find seemed to suggest a setter function. This won't work for me because my module will have logic that depends on a global variable being set, and the setter cannot be called without the module being instantiated with code that relies on the "un-set" variable.
Specifically I am trying to 'module-ize' a d3 graph to be generally useful, and want to be able to instantiate the module with a data array parameter.
EDIT: For a d3 graph, it turns out module pattern works best. There is nothing that needs to be instantiated with a data argument that cannot just be passed later in the update function...
But generally, if I have this module:
var thing = (function () {
var a = [1,2,3];
var l = a.length;
var doit = function () {
console.log('your array has '+l+' elements');
}
var doit2 = function () {
console.log('your array contains: '+a.toString());
}
return {
doit: doit,
doit2: doit2
}
})();
How can I pass an array to use in place of var a?
var thing = (function(param) { var a = param; /* etc. */ })(dataArray);?.init()or setter method. You could just setato an empty array by default, set it to the real value within the.init()or setter method, and if the client code tries to calldoit()without calling.init()then it'll get what it deserves.