exports
是module.exports
的一个引用,只是为了用起来方便。当你想输出的是例如构造函数这样的单个项目,那么需要使用module.exports
。
使用exports
circle.js
var PI = Math.PI;
exports.area= function(r){
return PI*r*r;
}
exports.circumference =function(r){
return 2*PI*r;
}
app.js
var circle = require('./circle.js');
console.log('the circle of radius is 5'+circle.area(5));
console.log(circle.circumference(5));
使用moduls.exports
circle.js
var PI = Math.PI;
module.exports = function(r){
this.radius = r;
this.area = function(){
console.log('the circle is area ='+PI*this.radius *this.radius);
}
this.circumference = function(){
console.log('this circle is circumference ='+2*PI*this.radius);
}
}
app.js
var Circle = require('./circle.js');
var c = new Circle(5);
c.area();
c.circumference();
来源:oschina
链接:https://my.oschina.net/u/1582832/blog/401700