最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js pass variable to module vs pass variable to every module function - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Apr 5, 2013 at 12:14 m03geek asked Apr 5, 2013 at 12:00 m03geekm03geek 2,5381 gold badge22 silver badges41 bronze badges 1
  • It sounds like you're asking about dependency injection, which you might think of as "passing a variable to a module". – candieduniverse Commented Oct 6, 2013 at 22:06
Add a ment  | 

1 Answer 1

Reset to default 6

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?

发布评论

评论列表(0)

  1. 暂无评论