调用父模块内部的功能让我们说我有一个名为parent.js
用下面的源代码文件:的NodeJS - 从孩子模块
var child = require('./child')
var parent = {
f: function() {
console.log('This is f() in parent.');
}
};
module.exports = parent;
child.target();
和一个叫child.js
用下面的源代码文件:
var child = {
target: function() {
// The problem is here..
}
}
module.exports = child;
和我使用下面的命令执行该文件:
node parent.js
这个东西是,我想直接在child.js
里面执行f()
而不用任何require(...)
声明。此前,我想在child.js
执行内部target()
这样的说法:
module.parent.f()
和
module.parent.exports.f()
,但它不工作。奇怪的是,当我执行console.log(module.parent.exports)
内child.js
,以下输出出现:
{ f: [Function] }
那么我为什么不能直接调用f()
?