Returning function object or anonymous object with same attributes

I am unable to comprehend whether the following two styles have any difference, which is more prefferable or are they just visually different.

STYLE 1 - Exporting a anonymous function that returns a javascript object with functions as attributes

module.exports = function(logger,db,config){

    return {
        getAByB : function(b,cb){
            logger.log(b);
            cb(undefined,'someting');
        },


        getName : function(cb){
             db.fetch(function(res){
                 cb(res)
             });
        }
    }
}

STYLE 2 - Exporting a function with attributes (honestly i don't understand why function name is used in return)

module.exports = function Something(logger,db,config){

        Something.getAByB = function(b,cb){
            logger.log(b);
            cb(undefined,'someting');
        }


        Something.getName = function(cb){
             db.fetch(function(res){
                 cb(res)
             });
        }

        return Something;
}

Also please explain what this 2nd style is trying to achieve.

Both would give you the same output, There is just a small difference that the second approach requires more file size due to writing the function name.

look at the following two files, one.js

module.exports = function some(width){

 return{  area : function() {
      return width * width;
    }
};


};

two.js

module.exports = function some(width){

    some.area= function() {
      return width * width;
    };

  return some;


};

you can log the following in server.js by requiring both these file one by one with following code.

    var mySquare =  square(2);
console.log('The area of my square is ' + mySquare);

The output you see on console for both the files is the area function,

for one.js the

The area of my square is
function some(width){

    some.area= function() {
      return width * width;
    };

  return some;


}

and for two.js It will just

The area of my square is [object Object]

This returned object is only this object

{  area : function() {
      return width * width;
    }
}

So when you compare the first approach is far better in terms of memory

Both are not the same.

The first will create a new object with the methods and properties you've specified. This will only be accessible as the return value.

var x = module.exports(1, 2, 3); // Object {getAByB: function...
console.log( module.exports.getAByB ); // undefined
module.exports(1, 2, 3).getAByB(4, 5); // works

The second will set all the methods to be chainable calls on the module.exports method itself as well as an instantly-chainable object:

var y = module.exports(1, 2, 3); // function Something...
console.log( module.exports.getAByB ); // function getAByB...
module.exports(1, 2, 3).getAByB(4, 5); // works

Both versions return a function which takes (logger,db,config) arguments, and then returns a value with getAByB and getName methods. However, there are potentially important differences.

The first version returns a brand new object each time. The second version returns a reference to the same object each time, and overwrites the object's methods! Consider the following code (I've assumed that the code examples are a module called "database-connector"):

var databaseConnector = require("database-connector");

var db1Logger = console.log.bind(console, "Db1 log:");
var db1 = ...;
var db1Connection = databaseConnector(db1Logger, db1, {});

var db2Logger = console.log.bind(console, "Db2 log:");
var db2 = ...;
var db2Connection = databaseConnector(db2Logger, db2, {});

With the first version of the module, this will work fine. However, with the second version of the code, db1Connection and db2Connection will both be the same value! If you call db1Connection.getAByB or db1Connection.getName, then db2Logger and db2 will be used instead because db2Connection's initialization overwrote db1Connection's methods.

Another difference is that with the second version of the module, db1Connection and db2Connection are also both equal to the databaseConnector function, and can be called directly to overwrite their methods again. I don't believe that would be a useful feature or was intended.