Home >Web Front-end >JS Tutorial >How I do: export/import?

How I do: export/import?

Susan Sarandon
Susan SarandonOriginal
2025-01-12 16:30:42782browse

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}

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(){}

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';

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()

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!

The above is the detailed content of How I do: export/import?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn