简体   繁体   English

Node.js中导出功能的最佳实践是什么

[英]What is the Best practice of exports function in Nodejs

I am learning nodejs , I find two way to exports our function in Nodejs , but I can not find what is difference between in those The first is 我正在学习nodejs,我发现了两种在Nodejs中导出函数的方法,但是我找不到两者之间的区别。

module.exports.UserService = (function () {
return {
      getUser:getUser
}
})()

And another 还有一个

var getUser=function(searchInfo,res){}
module.exports.getUser=getUser

Is there any disadvantage or advantage of using , or any other best practice for exports function 使用出口功能是否有任何不利或优势?

I always find it better to use the first notation (exporting the object via its reference) because: 我总是觉得最好使用第一种符号(通过引用导出对象),因为:

  1. it allows you to build the object incrementally, and 它允许您逐步构建对象,并且
  2. allows you to reference the object within itself. 允许您引用其内部的对象。

eg: 例如:

var Obj = {};
Obj.attrs = { "prop1": "val1", "prop2": "val2" };
addSomeProperties(Obj); /* possibly based on Obj.attrs */
module.exports = Obj;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM