在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));
您的兩種模式對於所有意圖和目的都完全相同。 – 2016-12-03 13:22:31