js基础module.exports、exports、prototype说明

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~

module.exports  我的理解是:你把什么东西赋给了module.exports,require后就会得到什么东西


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~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值