node module.exports和exports的区别

一、exports实际最终调用的是module.exportsjavascript

二、若是module.exports不具有任何属性和方法,exports中的属性和方法都会赋给module.exports;java

若是module.exports自己具有任何属性和方法,exports中的属性和方法都会被忽略。node

test.jsspa

test2.js对象

node test2.js //  TypeError: data.hello is not a functionblog

三、module.exports能够将对象实例化为其余类型;能够是任何合法的javascript对象--boolean, number, date, JSON, string, function, array等等ip

test.jsstring

test2.jsio

node test2.js //  output:   testfunction

 

 test.js

test2.js

node test2.js // output:   test2

 

若是你想你的模块是一个特定的类型就用Module.exports。若是你想的模块是一个典型的“实例化对象”就用exports。

相关文章
相关标签/搜索