exports和module.exports
是node.js两种对外暴露的方式。
注意
exports返回的是对象,可以直接调用。
module.exports返回的是一个类,需要new对象之后才可以调用。
exports
先创建一个module_a.js文件,用exports写对象

再创建一个main.js文件,写入module_a.js的调用

两个文件写在同一目录下,然后在bash上先cd到文件的位置,然后运行main文件(node
main.js)。结果如下:

其中exports作为对象在main.js中调用
module.exports
先创建一个person.js文件,用module.exports写函数

再创建文件main.js(之前创建过,就直接在文件中写),调用person.js。

然后同上,打开bash,cd到文件目录,运行main文件。结果如下。

其中Person作为一个类被调用,需要先new在调用。
本文章仅做为个人学习所用,希望对你有帮助。
网友评论