简体   繁体   English

在NodeJS的构造函数中使用`require`是不好的做法吗?

[英]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 language const 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.

相关问题 构造函数内部的构造函数 - 糟糕的做法? - constructor inside constructor - bad practice? 在while循环内使用await是否是个坏习惯 - Is it a bad practice to use await inside a while loop 在构造函数中使用 FormBuilder 是一种不好的做法吗? - Is using FormBuilder in the constructor a bad practice? 在 nodejs 中,使用基于 class(使用导入)或基于 function(使用 require)编程的最佳实践 - in nodejs which the best practice to use class based (using import) or function based ( using require) programming 让构造函数返回 Promise 是不好的做法吗? - Is it bad practice to have a constructor function return a Promise? NodeJS里面的'res没有定义' - Nodejs 'res is not defined' inside require 使用全局 XMLHttpRequest 是不好的做法吗? - Is it bad practice to use a global XMLHttpRequest? 像这样“const app = require('express')()”这样的开始快递被认为是不好的做法吗? - Is starting express like this “const app = require('express')()” considered bad practice? 页面中有多个js文件,每个文件都以require()开头:不好的做法还是可以吗? - Multiple js files in page, each starting with require(): bad practice or OK? 要求php文件而不是链接到js和css是不好的做法吗? - is it bad practice to require php files rather than link to js and css?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM