【发布时间】:2018-10-20 20:02:00
【问题描述】:
我希望我的模块 something.js 依赖于配置,但我不希望它依赖于配置本身 require,我希望我的编辑器继续能够分析模块并显示自动完成。有没有一种干净的方法可以做到这一点?不幸的是,这是一个让编辑感到困惑的解决方案。
class Something {
constructor (options) {
...
}
method () {
...
}
}
module.exports = options => module.exports = exports = new Something (options);
并在使用中:
// First use
const something1 = require ('./something')(options);
// All subsequent uses (expecting something1 to deep equal something2)
const something2 = require ('./something');
【问题讨论】:
-
那么,在随后的调用中,您会期望相同的实例吗?
-
const something = require ('./something');是否也希望模块使用这些选项进行初始化? -
@james 和 femioni - 是的,抱歉不清楚,将编辑
标签: javascript node.js module require