Node.jsでモジュールを作成するときに、「静的」と呼ばれる複数の機能を持つ、つまりnew
キーワードを使用しないモジュールを作成するときに使用するより良いパターンはどれですか。私が逃しているES6に相当するものはありますか?node.jsモジュールは名前付き関数またはオブジェクトをエクスポートする必要がありますか?
パターン1
// math.js
module.exports.add = (x, y) => {
return x + y;
}
module.exports.multiply = (x, y) => {
return x * y;
};
// app.js
const math = require('./math.js');
console.log(math.add(2, 3));
パターン2
// math.js
module.exports = {
add: (x, y) => {
return x + y;
},
multiply: (x, y) => {
return x * y;
}
};
// app.js
const math = require('./math.js');
console.log(math.add(2, 3));
あなたの2つのパターンは、すべての意図と目的でまったく同じです。 –