Better to export an object containing function, or just export multiple functions in ES6 (Is there a convention?)

Better to export an object containing function, or just export multiple functions in ES6 (Is there a convention?)



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.




2 Answers
2



Going forward, it's probably better to export multiple functions, as tree shaking allows module bundlers to eliminate dead code based on whether it's been imported or not.



If you export one large object, it's not always possible to use static analysis to tell which functions need to be kept and which can be safely discarded.



If you export multiple named functions, then the module bundler can analyze the AST then safely whitelist the functions which you are actually importing.



If you have as you say 50-100 functions that you want to expose through your utils file then I'd say go with the named exports


export function add()



Since otherwise every time an import of utils would take place you'd import the all of the functions. This might be what you want sometimes but most likely there'll only be a handful of useable functions for any given module.
If you want all of the functions a simple import * as utils from './utils'; would suffice.


import * as utils from './utils';



However there is nothing that doesn't say you can use both patterns if you want to safe guard against it.


export function add() ;

const utils =
add: add
;
export default utils;



Above is both valid and quite common as well.



Just remember as of Babel 6.x when developing a library using the export default will actually produce an object (the correct way) containing a default property on which your exported object will be attached to.


export default


default


import utils from './utils';

console.log(utils);
// default: yourUtilsObject






Thank you! You've given me exactly the explanation I was looking for. I was already you using the first syntax and I think I will continue it. Didn't know about the default thing too. So thanks for that.

– charrondev
Jan 11 '16 at 5:50



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)