1

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?

4
  • var thing = (function(param) { var a = param; /* etc. */ })(dataArray);? Commented Aug 3, 2016 at 3:22
  • 'the setter cannot be called without the module being instantiated with code that relies on the "un-set" variable' - In your example module there would be no problem adding an .init() or setter method. You could just set a to an empty array by default, set it to the real value within the .init() or setter method, and if the client code tries to call doit() without calling .init() then it'll get what it deserves. Commented Aug 3, 2016 at 3:25
  • If it's a module, a static global singleton, it makes no difference whether you pass it as an argument to the IIFE or simply put it inside the body. If you want it to be resusable, make it a factory or constructor function instead. Commented Aug 3, 2016 at 4:09
  • @Bergi I think factory function is the answer. I was avoiding a constructor function because applying "this." correctly with a lot of d3 code would be tricky. Commented Aug 3, 2016 at 4:15

2 Answers 2

2

You can do something like this (notice the IIFE drops out):

var thingCreator = function (param) {
    // your code
    return {
        // your module object
    };
};

Then call it like:

var thing = thingCreator(a);
Sign up to request clarification or add additional context in comments.

2 Comments

I think this might work, is this really just a factory function?
It is just a factory function. You could also pass in the argument to the IIFE, depending on when you need the value to be supplied.
0

How can I pass an array to use in place of var a?

I know this question is quite old, but I prefer to drop an answer for anybody who may need to pass data to Module Patterns dynamically.

Simply, add a function inside your Modular Function to update the a array dynamically.

var thing = (function () {
  // default empty array
  var a; 
  // update
  var updateDynamically = function (array) {
    a = array;
  }
  // check for count
  var getCount = function () {
    return a.length;
  }
  return {
    updateDynamically,
    getCount
  }
})();

Then at runtime, call the function as:

thing.updateDynamically([1,2,3]); // a is updated
thing.getCount(); // console >>> 3

Please note that this is for demonstration purposes only and you should update the answer to match the process you're trying to achieve. Also note that this example gives a direct access to update/reset the a array, this can be dangerous to use outside the scope of this simple example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.