在此页面(http://docs.nodejitsu.com/articles/getting-started/what-is-require)上,声明“如果要将导出对象设置为函数或新对象,则必须使用module.exports对象。”
我的问题是为什么。
// right
module.exports = function () {
console.log("hello world")
}
// wrong
exports = function () {
console.log("hello world")
}
我console.logged结果(result=require(example.js))和第[Function]一个是{}。
您能否解释其背后的原因?我在这里阅读了这篇文章:Node.js中的module.exports与export。它很有帮助,但没有解释以这种方式设计它的原因。如果直接返回出口参考书会不会有问题?
myTest.js
exports并且module.exports是相同的,并且是对同一对象的引用。您可以根据需要通过两种方式添加属性。