node中exports和module.exports的区别
- module.exports 导出的是对象,模块的本身,通常需要new对象
- exports导出单一的函数或者变量
module.exports
moudle.exports= [function name]
在currency.js
中:
//定义
class Currency{
constructor(rate){
this.rate = rate;
}
candidaToUs(us){
return us/this.rate;
}
}
module.exports = Currency;
在index.js
中调用:
//引用模块
const Currency = require('./currency');
const a = new Currency(10);
console.log(a.candidaToUs(100));
exports
exports.[function name] = [function name]
在currency.js
中:
//定义模块
function candidaToUs(us){
return 100/us;
}
exports.candidaToUs = candidaToUs;
在index.js
中调用:
//引入模块
const currency = require('./currency');
let a = currency.candidaToUs(10);