module.exports :
举个栗子:
1、
//a.js
module.exports = ['aaa',18]
//b.js
var a= require('a')
console.log(a[1]) //输出18
2、
//a.js
module.exports =function(){
this.show=function(){
console.log('hahah~');
}
}
//b.js
var a= require('a');
var obj = new a();
obj .show();//输出hahah~
exports :
//a.js
exports.show =function(){
console.log('hahah~');
}
//b.js
var a= require('a');
a.show();//输出hahah~
exports已经是一个对象,你可以向这个对象里面添加属性,在require后就得到的是这个exports对象。但是你不能给exports赋一个新对象,比如exports={}还需要注意的是如果module.exports已经有内容了,那么exports的所有操作都会失效,切记
再说一下prototype,prototype是干什么用的呢,它是在原型中添加属性,原型就像c++中的父类一样,我再来举个栗子
1、
//a.js
module.exports =function(){
}
module.exports.prototype.show = function(){
console.log('hahah~');
}
//b.js
var a= require('a');
var obj = new a()
obj.show()//输出hahah~
最后,说一下类方法,说到类,那肯定是用module.exports了。栗子在此
1、
//a.js
module.exports =function(){
}
module.exports.show = function(){
console.log('hahah~');
}
//b.js
var a= require('a');
a.show()//输出hahah~