亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

終于講清楚了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下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消