What method to export various functions to node?

So, I create a database layer and perform standard CRUD operations (Create, Read, Update, Delete) as functions. I am working on how to export each function.

Method 1

function func1 () {}
function func2 () {}

module.exports = {
    "func1": func1,
    "func2": func2
}

Method 2

var exporting;
exporting.func1 = function() {};
exporting.func2 = function() {};

module.exports = exporting;

or, just do it directly:

module.exports.func1 = function() {};
module.exports.func2 = function() {};

Method 3

export func1 = function() {}
export func2 = function() {}

I am sure that this will not harm any method, but what are the pros and cons (if any) of each of them?

+4
source share
1 answer

According to your examples, example # 1 and # 2 is the same as it exportsis an object. You are changing the way you add records to this object.

It:

const object = {};
object.func1 = function () { return 'hello'; };
object.func2 = function () { return 'bye'; };

You can write like this:

const object = {
  func1: function () { return 'hello'; },
  func2: function () { return 'bye'; }
};

ES2015 V8 ( Node.js). babel babel-preset-es2015.

CommonJS ES2015:

CommonJS , ES6 . : CommonJS , ES2015 .

: ES6? | 2ality.com

+3

Source: https://habr.com/ru/post/1664359/


All Articles