DEV Community

Aad Pouw
Aad Pouw

Posted on

How I do: export/import?

(First, it's about of how I do things, no must(not) do this or that here!)

I know there are many more ways to deal with javascript module export/import but this is of how I use it mostly!

EXPORT

How I'm not doing it and why?

function foo(){}
function bar(){}
function other(){}
export {foo,bar,other}
Enter fullscreen mode Exit fullscreen mode

At this way, the file has to be maintained. As soon as there are functions changing/added or removed you have to spend time to update this list X

How I do it then and why?

export function foo(){}
export function bar(){}
export function other(){}
Enter fullscreen mode Exit fullscreen mode

That might be clear, there is nothing to be maintained here V

IMPORT

It depends, if there are only one or two functions to be imported then I do it this way:

import {foo,bar} from './path/to/let/say/functions.js';
Enter fullscreen mode Exit fullscreen mode

If it is more then that, same story as by export. It has to be maintained and there is no need for that. X

How I do it then and why?

import * as FT from './path/to/let/say/functions.js';
//using it
FT.foo()
FT.bar()
FT.other()
Enter fullscreen mode Exit fullscreen mode

This way, it is always up-to-date, and no maintenance required V

 About wildcard/namespace

I make sure it is in uppercase ,short and reflects the imported filename

So in this example FT is short and reflects the imported file Func-Tions.js

That's it about my use of javascript module export/import.

My first post here and I have more in mind but for another time!

Top comments (0)