調用父模塊內部的功能讓我們說我有一個名爲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()
?