I have a question about the module
keyword in JavaScript.
I am familiar with:
export { name1, name2, …, nameN };
export expression;
export function;
export const
..
But what is the significance of export module SomeModuleName {..}
in JavaScript What is it used for and what are its advantages? I am considering if this pattern is helpful to learn.
In my case, I have a folder in my project called api
, in which I keep two big files, namely rest.class.ts
and stream.class.ts
. It seems reasonable to me to consider these as part of a bigger “ApiModule”. So what I tried was create another file called api-module.ts
.
So the file structure that I have looks like this:
api (folder)
- stream.class.ts
- rest.class.ts
- api.module.ts
In the api.module.ts
file I did the following:
import { RestClient } from "@api/classes/rest-client.class";
import { StreamClient } from "@api/classes/stream-client.class";
export module ApiModule {
export const getRestInstance = RestClient.getInstance;
export const getStreamInstance = StreamClient.getInstance;
}
In thise case, getInstance
are public static
members of the respective classes.
Doing this seems logical to me in semantic and intuitive terms. But my question is: what have I achieved by doing this? I could have done the same thing in many other ways.
It really begs the question of the special properties of the module
keyword. I feel that I am not using all of its powers but I have not found a good guide/explanation or online documentation on how it is supposed to be used. And most libraries I have seen simply use an index.js
file to bundle imports.
Can someone explain all of this to me?
Thank you so much!