node module.exports和exports的区别

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

2、如果module.exports不具备任何属性和方法,exports中的属性和方法都会赋给module.exports;

如果module.exports本身具备任何属性和方法,exports中的属性和方法都会被忽略。

test.js

test2.js

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

3、module.exports可以将对象实例化为其他类型;可以是任何合法的javascript对象--boolean, number, date, JSON, string, function, array等等

test.js

test2.js

node test2.js //  output:   test

 

 test.js

test2.js

node test2.js // output:   test2

 

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

 

posted @ 2017-05-20 16:16  IceU  阅读(124)  评论(0)    收藏  举报