1

I have a javascript module and I import it at the beginning of my main / main code and in it I execute a forEach, in this main code there is a loop that calls the methods of the module:

results.forEach(function(result) {
     for(const item of items .......){
        exampleModule.metodo(result, item)
     }
});

So far so good, the problem is that the values of the module are being mixed, the module is not dynamic according to the item Y of the result X, it kind of "disturbs" everything ...

I thought of a way to solve it, I do not know if it is the best practice, declare const exampleModule = require ('./modules/example.js') within an object and give that object a reference, such as item ID , since each ID will be unique:

let objetos = {}
results.forEach(function(result) {
     for(const item of items .......){
        objetos[item.id] = require('./modules/example.js')
        objetos[item.id].metodo(result, item)
     }
});

In short, I want each loop item to create an instance of the module without it being interfered with, if I instantiate the module in the item of position 0, everything in the module should only be used by that item 0, if it is item 1 the same thing, 1 n can change values of the module that is being used by position 0 and so on, is it possible? Can someone please help me?

Vinicius Aquino
  • 697
  • 13
  • 27

2 Answers2

1

I had the same idea, and checked it once, but it's basically a singleton what is returned by the require() function.

if you have

jQuery = require('jquery');
jQuery.fn.foobar = function() {
   this.css({color:red});
}

and then in another file you can do

$ = require('jquery');
$('hello').foobar();

The only condition is that the string you use to require() is the same case.

require('libs/myModule.js') != require('libs/mymodule.js');

behind the scenes a map is created:

 objects = {
    'jquery' : ....
    'add' : ....
    'strcasecomp' : ....
    '/www/htdocs/site/libs/myModule.js' : ...
 }

what happens when you require() is that if the object exists in the map, it's returned. If it doesn't exist, the relevant file is found, instantiated, mapped and returned.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Oh I understood, thank you very much for your answer but I think the answer from above where we call the assign method can solve my problem. – Vinicius Aquino May 08 '19 at 12:47
  • 1
    @viniciussvl You had probably a singleton problem, where all the objects in your array were the same instance. When you modified one item, it was modified in all. I just wanted to illuminate the underlying mechanics for you :-) I find it mostly useful to know "how" something works, so you can better work with it. – Tschallacka May 08 '19 at 12:49
  • Yes I understood it perfectly, if it changes in one changes at all, I was just wondering how to solve this. – Vinicius Aquino May 08 '19 at 12:51
  • 1
    @viniciussvl the `Object.assign()` works to copy an objects properties, instantiating also works `new Foobar()` if they are instantiateable classes or functions. it all depends on the implementation of the library. Always check what is expected. You might sometimes need [Object.create()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) or [new Object()](https://stackoverflow.com/questions/4166616/understanding-the-difference-between-object-create-and-new-somefunction#17952160) – Tschallacka May 08 '19 at 12:54
1

Node.js caches the modules which are imported. So there's no side effect of requiring them in a loop. The caching is explained here in docs.

And yes, it's not best practice to import modules deep in the code as it's considered less readable. You can always do something like:

let objetos = {}
const exampleModule = require('./modules/example.js');
results.forEach(function(result) {
     for(const item of items .......){
        objetos[item.id] = Object.assign({}, exampleModule);
        objetos[item.id].metodo(result, item)
     }
});
tbking
  • 8,796
  • 2
  • 20
  • 33
  • How cool, it copies all the properties of the module! I did not know I had this function, does that solve the issue of mixing the values in the module? – Vinicius Aquino May 08 '19 at 12:39