简体   繁体   English

在ES6 Node.js中,如何在任何地方提供类?

[英]In ES6 Node.js, how do I make a class available everywhere?

In my ES6 Node.js application, I have a file with a bunch of custom error classes, ie 在我的ES6 Node.js应用程序中,我有一个包含一堆自定义错误类的文件,即

class FirstCustomError extends Error {
  constructor(message) {
    super(message);
    this.name = "FirstCustomError";
  }
}

class SecondCustomError extends Error {
  constructor(message) {
    super(message);
    this.name = "SecondCustomError";
  }
}

// ...

How can I make these classes available everywhere in the application? 如何使这些类在应用程序中的任何地方可用? Ideally, I don't have to import them in every file I need them in, and I don't have to resort to throw new global.errors.FirstCustomError('error message') . 理想情况下,我不必将它们导入需要的每个文件中,也不必诉诸于throw new global.errors.FirstCustomError('error message')

That is, ideally, I can just do throw new FirstCustomError('error message') . 也就是说,理想情况下,我可以throw new FirstCustomError('error message')

You can indeed just put them on the global object : 您确实可以将它们放在global对象上

global.FirstCustomError = class extends Error {
  constructor(message) {
    super(message);
    this.name = "FirstCustomError";
  }
};

global.SecondCustomError = class extends Error {
  constructor(message) {
    super(message);
    this.name = "SecondCustomError";
  }
};

You would need to ensure that this happens before any other module is using them, ie you'd put it in a module that is required once at the beginning of your main script. 您需要确保在其他模块使用它们之前发生这种情况,即,将其放在主脚本开始处需要的模块中。 An even better (more reusable) approach would be to export them as normal from your errors module, and then do 更好(更可重用)的方法是从错误模块中正常导出它们,然后执行

Object.assign(global, require('./errors.js'));

in your main script to install all exports as globals. 在您的主脚本中将所有导出安装为全局变量。


Disclaimer : Of course, this is a horrible practice! 免责声明 :当然, 这是一种可怕的做法! Don't do that! 不要那样做! Just do explicitly import them everywhere you need them. 只需将它们明确导入到您需要的任何地方即可。 It's a breeze with ES6 module syntax, and it makes everything much clearer. ES6模块语法轻而易举,使一切变得更加清晰。 Also it is unlikely that you have so many custom errors in your application that are of truly cross-cutting concerns and not tied to specific functionality, in which case you shouldn't put them all in the same module anyway. 同样,您的应用程序中不可能有太多自定义错误,这些错误确实是跨领域的,并且与特定功能无关,在这种情况下,您无论如何都不应将它们全部放在同一个模块中。

You can add them to the first js file that runs, like app.js, and make sure they are in the global scope so they will be reusable from everywhere, but it's essential they are run first so they exist when some other code tries to call them 您可以将它们添加到运行的第一个js文件中(例如app.js),并确保它们在全局范围内,以便可以在任何地方重用,但必须首先运行它们,以便在其他一些代码尝试存在时它们存在,这一点很重要给他们打电话

global.customErrFunc = class FirstCustomError extends Error {
   constructor(message) {
     super(message);
     this.name = "FirstCustomError";
   }
}
throw new customErrFunc('Custom error')

Normally to have a class accessible you need to export it like this: 通常,要访问一个类,您需要像这样导出它:

exports.NameOfFunction = async (req, resp) =>{..... your code}; 

The async is not required! 不需要异步! just if you need to wait for a full response of a Promise i hope this helps! 就算您需要等待Promise的完整回应,也希望对您有所帮助!

What you are trying to achieve is possible as suggested by other posters, but it is not recommended . 你所试图实现可以通过其他海报的建议,但并不建议这样做。 An ideal way would be to export them into a module and import the module where ever you need it. 理想的方法是将它们导出到模块中,然后在需要时将其导入。

customErrors.js customErrors.js

export.modules.FirstCustomError = FirstCustomError
export.modules.SecondCustomError = SecondCustomError

I know this isn't what you are looking for, but opinionated development is good sometimes. 我知道这不是您要寻找的东西,但是有思想的发展有时是好的。 global assignment is a very frowned upon practice while working in modular environments. 在模块化环境中工作时, global分配在实践中非常皱眉。 You can use them in your code by importing. 您可以通过导入在代码中使用它们。

import { FirstCustomError } from './customErrors.js'

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

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