javascript - 使用 new 和存储在变量中的 ES6 类的 Node 出现错误

标签 javascript node.js subclass

我有以下 ES6 主类,它包含对其他 ES6 类的引用,这些类稍后在我的代码中动态调用,如下所示:

class Root {
   constructor() {
       this.refs = {
         ref1: require('ref1')
         ref2: require('ref2') 
       }
   }
}

module.exports = exports = new Root;

ref1 和 ref2 导出类 Ref1类 Ref2 的新实例。

稍后,我尝试通过执行以下操作来创建这些引用类之一的实例:

const Root = require('root');

class other {

 someFunc() {

   var ref = new Root.refs['ref1'](value);
 }

}

这总是以以下错误结束: 类型错误:this.actions[action.action] 不是构造函数

如何正确引用这些引用类?

最佳答案

new Root之后缺少()

如果导出设置为默认值,您可能还想尝试使用 require('ref1').default

关于javascript - 使用 new 和存储在变量中的 ES6 类的 Node 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41621090/

相关文章:

javascript - React 从给定的整数在 for 循环中渲染多个按钮

javascript - Q - 执行一系列 promise 并在 DAG 中定义它们之间的依赖关系

javascript - 自动完成和用户定义键盘

node.js - 我可以在 Heroku 托管的 node.js 应用程序中使用 New Relic 吗?如果没有,什么是监控的好选择?

parent-child - Kotlin:子类如何在 super 构造函数调用中使用父类的扩展功能?

javascript - 通过属性对数组内的数组重新排序的最佳方法

node.js - Mongoose 验证未调用

javascript - 如何在 node.js 事件监听器中获取事件名称?

ios - 子类化/扩展 UIView,以便所有其他 View (UIImageView、UITextField ...)在创建时继承

Python:在基类的方法中初始化一个新的派生类