简体   繁体   中英

Non-global variables everytime I call a module

These variables are in a module.exports file, and when I call the module multiple times, each instance shares the values. I don't want them to share values, each time I call the module, I want each call to have its own cycle number, original, newprod, etc.. Like multithreading.

I am in NodeJS on a Mac

cycle = 0; var original = []; var newprod = []; var proxylist = [];

NEW EDIT -----

I added a new constructor when calling and it didn't work. Not sure whats the problem they are still sharing the variables. Here is my code.

config.sites.forEach(function (site) { var mod = require( ./src/monitor ).init, mod1 = new mod(site); });

If you need to prevent overriding variables, you should limit them to a specific scope . A scope can be a function, or class.

For your case, you can create a class to maintain the variables isolated in each instance and then creating methods for updating their values. Here's an example I found .

Also, from es2015 you can create classes like in most languages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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