【问题标题】:Correctly export classes in node.js to extend them in other required files [duplicate]正确导出 node.js 中的类以在其他所需文件中扩展它们[重复]
【发布时间】:2017-07-07 18:09:52
【问题描述】:

我已经连续坐了 3 个小时,试图弄清楚我如何可能从一个文件中导出一个类,在其他文件中需要它(多次),以便可以扩展该类。

我当前的代码如下所示:

// base.js //
class Base {
    constructor() { ... }
}

exports = Base;

// extension.js //
var Base = require('./base.js');

class Extension extends Base {
    constructor() { ... }
}

我收到一个异常,告诉我“Base”不是构造函数。当我运行 console.log(Base) 时,我得到一个空对象。是我做错了什么,还是我的愿望不可能实现?

我会很高兴得到任何帮助!

【问题讨论】:

  • 你为什么不做module.exports = Base;?如果你使用 ES6,为什么不export default Base;
  • 这太尴尬了。我是如何错过这个模块的。*我完全不清楚。非常感谢,我现在要痛苦地哭了。

标签: javascript node.js ecmascript-6 require


【解决方案1】:

Node 还不支持正常的 ES6 导出。因此,如 cmets 中所述,需要 module.exports。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    相关资源
    最近更新 更多