1

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

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

abhaygarg12493
  • 1,565
  • 2
  • 20
  • 40

1 Answers1

0

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.

e.g.:

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