我正在使用module.exports来获取所需的类,在其中三个中,我之前需要调用构造函数,但是随后它们返回一个对象而不是一个函数。
如何使它们返回一个函数,以便可以像其他类一样使用这些类?
Handlers.js:
module.exports = {
Class: new (require("./Class"))(),
OtherClass: require("./OtherClass"),
...
}
index.js:
const handlers = require("../Handlers")
// This is called dynamically, which is why I'm using the module.exports
// handler is from a JSON object, let's say file is Class and event is a method within that class
handlers[handler.file][handler.event](data, arg2)
我希望第一堂课与第二堂课具有相同的类型。当前,第一个是对象,第二个是函数。我必须将参数传递给类,因此它必须是一个函数。
输出:(Handlers.js的console.log)
Class: Class { ... }
OtherClass: [Function: OtherClass]