2

I'm learning node.js and interested in is there any difference between following two cases. I.E. I have some variable myvar (like db connection or just constant string "test") that needed to be passed in many modules and submodules.

First case. Create modules, that accept that variable as a param:

submodule.js:

var option
  , submodule = {};
submodule.func = function(){
    ...
    var something = option;
    ...
}
module.exports = function(opts){
    option = opts;
    return submodule;
}

module1.js:

var option
  , submodule
  , module1 = {};
module1.func = function(){
    ...
    submodule.func();
    ...
    var something = option;
    ...
}
module.exports = function(opts){
    option = opts;
    submodule = require('./submodule')(opts);
    return module1;
}

In this case if submodule is used in several modules with same myvar value (i.e. 2 modules) submodule's module.exports function will be called 2 times. In node.js mans it said that "Modules are cached after the first time they are loaded". And I can't understand is this module cached or not.

Another case: That myvar can be passed as parameter to module functions. So code will look like:

submodule.js:

function func(option){
    ...
    var something = option;
    ...
};
exports.func = func;

module1.js:

var submodule = require('./submodule');
function func(option){
    ...
    submodule.func(option);
    ...
    var something = option;
    ...
};
exports.func = func;

So the question is: Is there any difference between this two cases or they are same?

m03geek
  • 2,429
  • 1
  • 19
  • 38
  • It sounds like you're asking about [dependency injection](http://stackoverflow.com/questions/130794/what-is-dependency-injection), which you might think of as "passing a variable to a module". – ekillaby Oct 06 '13 at 22:06

1 Answers1

5

I'm not exactly sure what you're asking here, but if you need to pass values into your modules you should make sure to export functions that accept parameters. When they say that a module is cached, it means that your module is only initialized once. Think of your module as an object:

var a = 1;
var b = 2;
console.log("init!");

module.exports.test = function(){
      console.log("this is a function!");
}

Here, a, b, and the first log will run only once. This is when the module is requested and is then cached. When you do a

 var example = require("module")

If it was never created it'll initialize a, b, and do the log message. If it was already created it will just give you a reference to what you exported. Each time you call:

 example.test()

It will output: this is a function!

But you will NOT get the a, b, and the first log run again.

Think of all statements not exported as private static variables of that object.

Here is another fully working example:

app.js

var s = require("./sample");
var y = require("./sample");
s.test();
y.test();
s.test();
console.log("finished");

sample.js

var a = 1;
var b = 2;
console.log("init!");
function test() {
    console.log("here! " + a);
    a++;
}
exports.test = test;

This all outputs:

init!
here! 1
here! 2
here! 3
finished

Does this help at all?

Shipow
  • 2,381
  • 1
  • 20
  • 28
devshorts
  • 7,602
  • 4
  • 44
  • 68
  • I'm asking about the difference between passing "static" vars into module and passing them into each function. The reason I'm asking about it is that with parametrized module.exports on require('./submodule') it calls module.export's function multiple times (each time on require). – m03geek Apr 05 '13 at 22:41
  • Right, thats what I'm saying. The export is just a function reference, but it can close over "statics" that are declared outside of exported values. – devshorts Apr 05 '13 at 23:06
  • Ok. So can you give short answer (your own opinion): Is there any difference between this cases (yes/no), which one do you prefer to use? Thanks. – m03geek Apr 06 '13 at 16:28
  • 1
    The second way is preferred. Do a require to get a reference to the module, then use it to call functions. No reason to do extra requires inside of functions just to pass arguments to exports – devshorts Apr 07 '13 at 03:18