简体   繁体   English

循环内导入模块是一个坏习惯吗?

[英]Import module inside the loop is a bad practice?

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: 我有一个javascript模块,并将其导入到主/主代码的开头,并在其中执行forEach,在此主代码中,有一个循环调用该模块的方法:

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 ... 到目前为止,很好的问题是,模块的值正在混合,模块根据结果X的项Y不是动态的,有点“扰乱”了一切...

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: 我想到了一种解决方法,我不知道这是否是最佳实践,在对象内声明const exampleModule = require('./modules/example.js')并为该对象提供引用,例如项目ID ,因为每个ID都是唯一的:

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? 简而言之,我希望每个循环项都创建一个模块实例而不会受到干扰,如果我在位置0的项目中实例化该模块,则该模块中的所有内容都只能由该项目0使用,如果它是item 1同样,1 n可以更改位置0使用的模块的值,依此类推,这可能吗? Can someone please help me? 有人可以帮帮我吗?

I had the same idea, and checked it once, but it's basically a singleton what is returned by the require() function. 我有相同的想法,并检查了一次,但是基本上它是由require()函数返回的单例。

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()的字符串是相同的情况。

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. 当您require()时发生的情况是,如果该对象存在于地图中,则将其返回。 If it doesn't exist, the relevant file is found, instantiated, mapped and returned. 如果不存在,则找到,实例化,映射并返回相关文件。

Node.js caches the modules which are imported. Node.js缓存导入的模块。 So there's no side effect of requiring them in a loop. 因此,将它们循环需要没有副作用。 The caching is explained here in docs . 缓存在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)
     }
});

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

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