【发布时间】:2016-11-25 13:47:44
【问题描述】:
我正在阅读 Axel Rauschmayer 的 this article,特别想知道以下句子:
Node.js 样式的单值导出不起作用。在 Node.js 中,您可以 导出单个值而不是对象,如下所示: module.exports = 函数 () { ... } 如果你在模块 A 中这样做,你将无法使用模块 B 中的导出函数,因为 B 的变量 a 仍然 引用 A 的原始导出对象。
“A 的原始导出对象”是什么意思??
【问题讨论】:
-
这句话中指的是CommonJS系统,而不是NodeJS,原来的exports对象是你在
module.exports = function { ... }时试图覆盖的对象。换句话说,在B中,导入A将不会导入该函数,除非您将其分配给A中module.exports的属性。而在 NodeJS 中,您将获得直接分配给module.exports的任何值(如果有)。 -
@squint 感谢 cmets。 Nodejs 和 CommonJS 在
export方面有什么区别吗?但写的是In Node.js, you can export single values instead of objects, like this: module.exports = function () { ... } If you did that in module A, you wouldn’t be able to use the exported function in module B -
对不起,我想我在上面的评论中有点偏离。它指的是周期性依赖,所以我认为他们是说在这种情况下最终导入的内容会有限制。因此,如果您用 A 中的值覆盖
module.exports,该值导入 B,该值导入 A,在 B 中,您将无法访问来自 A 的覆盖exports值,因为 B 会在 之前 i> 发生了覆盖,所以它得到了module.exports(原始对象)的原始值,而不是被覆盖的值。
标签: javascript module commonjs