exports和module.exports
示例代码
定义一个helloModule模块
exports={
name:"猪八戒",
age:28,
sayName:function () {
console.log("我是猪八戒");
}}
,再定义一个另外一个js文件,引用helloModule中的变量和函数看结果
var hello = require("./helloModule");
console.log(hello.name);
console.log(hello.age);
hello.sayName();
此时运行控制台会报错,原因是没有写module.exports,将hellowModule模块的代码改成如下,再运行可以调用参数和函数
/***
* exports 和 module.exports
* - 通过exports只能使用.的方式来向外暴露内部变量
* exports.xxx = xxx
* - 而 module.exports既可以通过.的形式,也可以直接赋值
* module.exports.xxx = xxx
* moduel.exports = {}
*/
//不写module. 调用函数时会报错
module.exports = {
name:"猪八戒",
age:28,
sayName:function () {
console.log("我是猪八戒");
}
};