简体   繁体   English

节点定义了我需要在模块之间使用的唯一变量

[英]Node define unique variable that I need to use across the modules

Currently I am just passing my fileNamePrefix like that: 目前,我只是像这样传递我的fileNamePrefix

let shortid = require('shortid');
let fileNamePrefix = shortid.generate();

module1.run(fileNamePrefix); //generating a file based on `fileNamePrefix` `xxxxx.f1.json`
module2.run(fileNamePrefix); //generating a file based on `fileNamePrefix` `xxxxx.f2.json`
module3.run(fileNamePrefix); //generating a file based on `fileNamePrefix` `xxxxx.f3.js

Which I think in not quite right, I might need to pass more things to my modules later on, so I want to avoid to pass that as function params . 我认为不太正确,以后可能需要将更多内容传递给模块,因此我想避免将其作为函数params传递。

What is the best way to approach that in nodejs? 在nodejs中解决该问题的最佳方法是什么?

Will global object like global.fileNamePrefix = shortid.generate(); global.fileNamePrefix = shortid.generate();这样的global对象global.fileNamePrefix = shortid.generate();global.fileNamePrefix = shortid.generate(); will do in that case or would you approach that different? 在那种情况下会做还是会采取不同的方式? I just read that global is not good... 我刚刚读到global不好...

Just create module like that: 像这样创建模块:

// unicname.js
let shortid = require('shortid');
let fileName = shortid.generate();

module.exports = {fname: fileName};

//module1.js
const {fname} = require('./unicname.js');
....

Since the node.js caching the modules the value will be calculated only one time so you can get same value in all your modules. 由于node.js将模块缓存,因此该值将仅计算一次,因此您可以在所有模块中获得相同的值。

You can use either singleton approach or approach suggested by @Сергей Тертичный 您可以使用单例方法,也可以使用@СергейТертичный建议的方法

  1. Singleton : 单身人士:

     //shortid.js var fileSingleTon = module.exports = { fileNamePrefix: null, getFileNamePrefix: function() { return fileSingleTon.fileNamePrefix || fileSingleTon.generate() }, generate: function() { console.log('generating..'); return fileSingleTon.fileNamePrefix = 'your_file_prefix'; } } //module1.js var fileNamePrefix = require('./shortid').getFileNamePrefix(); //do stuff for module1 //module2/js var fileNamePrefix = require('./shortid').getFileNamePrefix(); //do stuff for module1 

    and so on.. 等等..

Even now you are calling require('./shortid').getFileNamePrefix(); 即使现在您正在调用require('./ shortid')。getFileNamePrefix(); multiple times, generate function is getting called only once. 多次,generate函数仅被调用一次。

  1. Node Caching approach : 节点缓存方法:

Consider you have shortid.js as following : 考虑您具有shortid.js,如下所示:

// A: block of code to do some manipulations

// B : module.exports = manipulation result.

So basically in node js "modules" core module which is responsible for giving us module.export functionality executes whatever is here above export(in abode example the part A) only for the first time and caches it even if you have required in in multiple other files. 因此,基本上在节点js的“模块”核心模块中,该模块负责为我们提供module.export功能仅首次执行export(在上面的示例中为A部分)以上的内容,即使您需要多次,也将其缓存其他文件。 However, it only executes the functions or block of code in every require which is inside export. 但是,它仅在导出内部的每个需求中执行功能或代码块。 So you can use this approach where your A block will have login to generate fileNamePrefix and then B just returns it. 因此,您可以使用此方法,其中您的A块将登录以生成fileNamePrefix,然后B仅仅返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM