1// helloworld.js
2
3export function helloWorld() {
4 return 'Hello World!';
5}
6
7// main.js
8
9import helloWorld from './helloworld.js';
10
11console.log(helloWorld());
1// something.js
2
3export const hi = (name) => console.log(`Hi, ${name}!`)
4export const bye = (name) => console.log(`Bye, ${name}!`)
5export default () => console.log('Hello World!')
6
7We can use import() syntax to easily and cleanly load it conditionally:
8// other-file.js
9
10if (somethingIsTrue) {
11 import('./something.js').then((module) => {
12 // Use the module the way you want, as:
13 module.hi('Erick') // Named export
14 module.bye('Erick') // Named export
15 module.default() // Default export
16 })
17}
1// module-name = module to import
2// defaultExport - Namespace to refer to the default export from the module.
3import defaultExport from "module-name";
4import * as name from "module-name";
5// exportN - Name of export to be imported
6import { export1 } from "module-name";
7// aliasN - Reasign export to new namespace
8import { export1 as alias1 } from "module-name";
9import { export1 , export2 } from "module-name";
10import { export1 , export2 as alias2 , [...] } from "module-name";
11import defaultExport, { export1 [ , [...] ] } from "module-name";
12import defaultExport, * as name from "module-name";
13import "module-name";
14var promise = import("module-name");
1//import it
2import Example from './file2';
3//Create an Instance
4var myInstance = new Example()
5myInstance.test()