【发布时间】:2014-10-17 19:06:39
【问题描述】:
我有 3 个文件:
孩子1:
var Parent = require('./Parent');
Parent['type'] = 'Child1';
module.exports = Parent;
孩子2:
var Parent = require('./Parent');
Parent['type'] = 'Child2';
module.exports = Parent;
家长:
module.exports = { 'parent' : 1 }
test.js:
var test1 = require('./Child1')
var test2 = require('./Child2')
结果:
{ parent: 1, type: 'Child2' }
----
{ parent: 1, type: 'Child2' }
为什么?
【问题讨论】:
-
正如 dystroy 所指出的,模块由路径缓存作为键。请注意,如果您需要来自同一个模块的两个对象/状态实例,那么您需要一个带有 new 的 JS 对象模式。
-
OP,你能解释一下你为什么不接受这个答案吗?缺少什么?
标签: javascript node.js