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。

相關文章
相關標籤/搜索