[英]Is it a bad practice to use `require` inside a constructor in NodeJS?
I am building a node app where a certain wordlist is required. 我正在构建需要特定单词列表的节点应用程序。 The wordlist is in a JSON file that looks something like this: 单词表在一个JSON文件中,看起来像这样:
{
"en":["foo", "bar"],
"gr": ["foo", "bar"]
}
Each key inside of the JSON file represents a different language. JSON文件中的每个键代表一种不同的语言。
The user has to pick a language when they create their object. 用户在创建对象时必须选择一种语言。 So I am thinking of importing the JSON file inside of the constructor like this: 所以我正在考虑像这样在构造函数内部导入JSON文件:
const list = require('./config/lang.json')[lang]
Where lang is a parameter passed to the constructor. 其中lang是传递给构造函数的参数。
Is this bad practice? 这是不好的做法吗?
I've heard people say that you should always use require
in the beginning of your code. 我听说有人说您应该在代码开头始终使用require
。 Should I just require the whole thing in the beginning of my code const list = require('./config/lang.json')
and then simply extract only the required language const wordlist = list[lang]
inside the constructor? 我是否应该只在代码const list = require('./config/lang.json')
的开头要求全部内容,然后仅在构造函数内部仅提取所需的语言const wordlist = list[lang]
?
Even though the code works the same, and require
calls are cached. 即使代码工作原理相同,并且require
调用也被缓存。 In your case there is no need for that extra function call on every new instance, so it will be faster (not that it matters in this case) to do the following: 在您的情况下,不需要在每个新实例上进行额外的函数调用,因此执行以下操作会更快(在这种情况下无关紧要):
const langs = require('./config/lang.json');
class MyClass {
constructor(lang) {
const list = langs[lang];
}
}
Another thing to notice, is that require
is synchronous, so if your JSON is specially large, the first time you instanciate MyClass
the event loop will be blocked. require
注意的另一件事是require
是同步的,因此,如果您的JSON特别大,则首次实例化MyClass
,事件循环将被阻止。 Using it at the very beginning it will probably have loaded before the server (or whatever you're doing) has started, so there would be no issue in the require
taking some time. 使用它在一开始就可能已经在服务器(或任何你正在做的)加载之前已经开始,因此将在没有任何问题, require
花一些时间。
So, yes, in my opinion, require
calls should be at the top, unless you know what you're doing, or you're loading a dynamic dependency. 因此,是的,在我看来,除非您知道自己在做什么或正在加载动态依赖项,否则require
调用应该位于顶部。
Is this a bad practice? 这是不好的做法吗?
Not really, require
has a cache so it doesn't matter a lot. 并非如此, require
具有高速缓存,因此无关紧要。
I've heard people say that you should always use require in the beginning of your code. 我听说有人说您应该在代码开头始终使用require。
Yes, that's a good practice so that one can easily spot the dependencies. 是的,这是一个好习惯,这样您就可以轻松发现依赖项。
Should I just require the whole thing in the beginning of my code
const list = require('./config/lang.json')
and then simply extract only the required languageconst wordlist = list[lang]
inside of the constructor? 我应该只在代码const list = require('./config/lang.json')
的开头要求全部内容,然后在构造函数内部仅提取所需的语言const wordlist = list[lang]
吗?
Yes, I would do that. 是的,我会这样做。 I'd only put require
inside the constructor if it was a dynamic dependency, like const wordlist = require(
./config/lang/${lang}.json )
. 如果它是动态依赖项,我只会将require
放入构造函数中,例如const wordlist = require(
./ const wordlist = require(
.json )
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.