This is a question of convention. I'm new to ES6 but I'm trying to make use of the module system. Is preferred/more common to export multiple functions from a single file, or export a single object containing these functions.
Example:
utils.js
export function add(num1, num2) {
return num1 + num2;
}
export function minus(num1, num2) {
return num1 - num2;
}
and use it like this:
import {add, minus} from 'utils.js';
vs
utils.js
const utils = {
add: (num1, num2) => {
return num1 + num2;
},
minus: (num1, num2) => {
return num1 - num2;
}
}
export default utils;
In a utils file that contains a 50-100 functions, it seems the second way would be the clear winner. But there's just something that feels wrong about it to me, and I don't know why.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…