每一个node.js文件都会创建一个module对象,同时,module对象会创建一个叫exports的属性,属性值为一个对象,初始值为{}。
module.exports={ }。
}
module.exports=hello //输出函数
bb.js文件
var hello=require("./aa"); //require请求到的是一个函数
hello()
exports对象实际上就是module.exports对象的一个引用,对exports添加属性就相当于对module.exports添加属性(
var hello=function () {
console.info("hello")
}
exports.hello=hello
bb.js文件
var hello=require("./aa"); //require请求到的是一个对象{hello:hello}
hello.hello()
总结:nodejs文件输出的module.exports的值
module.exports={ }。
module.exports是每个nodejs文件对外输出的值,require请求到的值就是module.exports这个值。
aa.js文件
var hello=function () {
console.info("hello")}
module.exports=hello //输出函数
bb.js文件
var hello=require("./aa"); //require请求到的是一个函数
hello()
exports对象实际上就是module.exports对象的一个引用,对exports添加属性就相当于对module.exports添加属性(
前提是module.exports和exports指向同一个对象),输出一个对象
aa.js文件var hello=function () {
console.info("hello")
}
exports.hello=hello
bb.js文件
var hello=require("./aa"); //require请求到的是一个对象{hello:hello}
hello.hello()
总结:nodejs文件输出的module.exports的值