目前,我在自己的文件中有4个子类。我要求它们都在同一个文件中。我想知道是否可以在一个模块中包含所有这四个类。目前,我正在像这样导入它们
var Jack = require('./Jack.js'); var JackInstance = new Jack(); var Jones = require('./Jones.js'); var JonesInstance = new Jones();
我想这样导入
var People = require('./People.js'); var JackInstance = new People.Jack();
甚至
var Jack = require('./People.js').Jack; var JackInstance = new Jack();
我的班级定义如下
class Jack{ //Memeber variables, functions, etc } module.exports = Jack;
您可以这样导出多个类:
例如 People.js
class Jack{ //Member variables, functions, etc } class John{ //Member variables, functions, etc } module.exports = { Jack : Jack, John : John }
并按照您正确提到的方式访问这些类:
var People = require('./People.js'); var JackInstance = new People.Jack(); var JohnInstance = new People.John();