可以从一个Nodejs模块导出多个类吗?

时间:2022-09-25 11:58:57

Currently, I have 4 Child Classes each in their own file. I'm requiring them all in the same file. I am wondering if I can contain all 4 of those classes in a single module. Currently, I'm importing them like this

目前,我在自己的文件中有4个子类。我要把它们都放在同一个文件里。我想知道我是否可以在一个模块中包含所有这4个类。目前,我像这样导入它们

var Jack = require('./Jack.js');
var JackInstance = new Jack();
var Jones = require('./Jones.js');
var JonesInstance = new Jones();

I'd like to import them like this

我想这样导入它们

var People = require('./People.js');
var JackInstance = new People.Jack();

Or even

甚至

var Jack = require('./People.js').Jack;
var JackInstance = new Jack();

My classes are defined like so

我的类是这样定义的

class Jack{
    //Memeber variables, functions, etc
}

module.exports = Jack;

2 个解决方案

#1


38  

Yes, you can export multiple classes.

是的,您可以导出多个类。

e.g. People.js

例如People.js

class Jack{
   //Memeber variables, functions, etc
}

class John{
   //Memeber variables, functions, etc
}

module.exports = {
  Jack : Jack,
  John : John
}

And, you can access these classes as you have correctly mentioned.

并且,您可以访问这些类,正如您所正确地提到的。

var People = require('./People.js');
var JackInstance = new People.Jack();
var JohnInstance = new People.John();

#2


40  

You can also do this in a shorter form, using destructuring assignments (which are supported natively starting from Node.js v6.0.0):

您也可以使用更短的格式,使用析构赋值(从Node开始支持这种方法)。js v6.0.0):

// people.js
class Jack {
  // ...
}

class John {
  // ...
}

module.exports = { Jack, John }

Importing:

进口:

// index.js
const { Jack, John } = require('./people.js');

Or even like this if you want aliased require assignments:

或者像这样,如果你想要别名,需要分配任务:

// index.js
const {
  Jack: personJack, John: personJohn,
} = require('./people.js');

In the latter case personJack and personJohn will reference your classes.

在后一种情况下,personJack和personJohn将引用您的类。

A word of warning:

一个字的警告:

Destructuring could be dangerous in sense that it's prone to producing unexpected errors. It's relatively easy to forget curly brackets on export or to accidentally include them on require.

破坏可能是危险的,因为它容易产生意想不到的错误。相对而言,很容易忘记导出中的花括号,或者不小心将它们包含在require中。

#1


38  

Yes, you can export multiple classes.

是的,您可以导出多个类。

e.g. People.js

例如People.js

class Jack{
   //Memeber variables, functions, etc
}

class John{
   //Memeber variables, functions, etc
}

module.exports = {
  Jack : Jack,
  John : John
}

And, you can access these classes as you have correctly mentioned.

并且,您可以访问这些类,正如您所正确地提到的。

var People = require('./People.js');
var JackInstance = new People.Jack();
var JohnInstance = new People.John();

#2


40  

You can also do this in a shorter form, using destructuring assignments (which are supported natively starting from Node.js v6.0.0):

您也可以使用更短的格式,使用析构赋值(从Node开始支持这种方法)。js v6.0.0):

// people.js
class Jack {
  // ...
}

class John {
  // ...
}

module.exports = { Jack, John }

Importing:

进口:

// index.js
const { Jack, John } = require('./people.js');

Or even like this if you want aliased require assignments:

或者像这样,如果你想要别名,需要分配任务:

// index.js
const {
  Jack: personJack, John: personJohn,
} = require('./people.js');

In the latter case personJack and personJohn will reference your classes.

在后一种情况下,personJack和personJohn将引用您的类。

A word of warning:

一个字的警告:

Destructuring could be dangerous in sense that it's prone to producing unexpected errors. It's relatively easy to forget curly brackets on export or to accidentally include them on require.

破坏可能是危险的,因为它容易产生意想不到的错误。相对而言,很容易忘记导出中的花括号,或者不小心将它们包含在require中。