为了账号安全,请及时绑定邮箱和手机立即绑定

终于讲清楚了nodejs中exports和module.exports的区别

标签:
JavaScript

最近正在学习nodejs,看到nodejs模块这块,发现nodejs模块有两种方式对外暴露方法
exports和module.exports

可是这两种使用起来到底有什么区别呢???

看了很多文章,长篇大论,始终没有讲清楚区别,自己也是看了很多,终于搞清楚了,给大家分享一下

根据使用方法来说

通常exports方式使用方法是:

exports.[function name] = [function name]

moudle.exports方式使用方法是:

moudle.exports= [function name]

这样使用两者根本区别是

**exports **返回的是模块函数

**module.exports **返回的是模块对象本身,返回的是一个类

使用上的区别是
exports的方法可以直接调用
module.exports需要new对象之后才可以调用

二话不说,撸代码!

1. exports方式

先创建一个exports_mode.js

var sayHello = function(){    console.log('hello')
}
exports.sayHello = sayHelloconsole.log(exports); 
console.log(module.exports);

然后写一个test.js调用下试试看

var exports_mode = require('./exports_mode')
exports_mode.sayHello()

输出:


exports_mode.png

发现此时exports和module.exports对象输出的都是一个sayHello方法,
为什么module.exports也有exports方法了,简单点理解就是

exports是module.exports的一个引用,exports指向的是module.exports

我们来验证下,在exports_mode.js最后一行添加一句代码

var sayHello = function(){    console.log('hello')
}
exports.sayHello = sayHelloconsole.log(exports); 
console.log(module.exports); 
console.log(exports === module.exports);

结果输出.png

发现console.log(exports === module.exports)返回的是true,
说明exports和module.exports是同一个对象

下来看看

2. module.exports方式

首先创建module_exports_mode.js

var sayHello = function(){    console.log('hello')
}module.exports = sayHelloconsole.log(module.exports); 
console.log(exports); 
console.log(exports === module.exports);

然后测试一下

var module_export_mode = require('./module_exports_mode')
module_export_mode.sayHello()

控制台输出.png

发现输出报错了!

为什么呢,因为我们的调用方式错了,一开始就说到了

**module.exports **返回的是模块对象本身

正确的调用

var module_export_mode = require('./module_exports_mode')new module_export_mode()

控制台输出.png


同时我们可以看到,输出的module.exports对象内容就是一个[Function],在javascript里面是一个类

使用这样的好处是exports只能对外暴露单个函数,但是module.exports却能暴露一个类

我们把module_exports_mode.js扩展一下

var xiaoming = function(name){    this.name = name    this.sayHello = function(){        return 'hello '+this.name
    }    this.sayGoodBye = function(){        return 'goodbye '+this.name
    }
}module.exports = xiaomingconsole.log(module.exports); 
console.log(exports); 
console.log(exports === module.exports);

然后测试

var xiaoming = require('./module_exports_mode')var xiaoming = new xiaoming('Lucien')console.log(xiaoming.sayHello())console.log(xiaoming.sayGoodBye())

控制台输出.png

使用方法和javascript的类创建对象一毛一样

exports.[function name] = [function name]
moudle.exports= [function name]

以上就是这两种方式的使用区别。


等等,还没完。。。

上面有提到

exports是module.exports的一个引用,exports指向的是module.exports

也就是说exports的方法module.exports也是一定能完成的

exports.[function name] = [function name]
moudle.exports= [function name]

所以,在使用上

** moudle.exports.[function name]   = [function name] **
**  是完全和 **
** exports.[function name] = [function name]  **
**  相等的   **

但是我们通常还是推荐使用exports.[function name],各司其职,代码逻辑清晰


作者:程序鱼
链接:https://www.jianshu.com/p/43b151089d29

点击查看更多内容
9人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消