10. The ES6 modules

The ES6 modules allow you to build Javascript applications structured into independent and reusable modules.
10.1. [import-01, export-01] scripts
The [import-01] script will use the [export-01] module. This module is defined as follows:
// default export of an unnamed object
export default {
data: 2,
do() {
console.log(this.data);
}
};
Comments
- The lines [2-7] define an unnamed object with the properties [data, do];
- line 2: the statement [export default] exports this object. It can therefore be imported;
The [export-01] module is used by the [import-01] script as follows:
'use strict';
// import a default exported object
import export01 from './export-01';
// using this object
export01.do();
// a default export can be imported under any name
import data from './export-01';
console.log(data.data);
Comments
- Lines 3 and 7 import the default exported object from the [export-01] module under two different names;
- Once an object is imported, it can be used as if it had been defined locally in the script;
Execution
[Running] C:\myprograms\laragon-lite\bin\nodejs\node-v10\node.exe -r esm "c:\Data\st-2019\dev\es6\javascript\modules\import-01.js"
2
2
10.2. scripts [import-02, export-02]
These scripts demonstrate the export of a named object.
The script [export-02] is as follows:
// default export of an object named
const data = {
data: 2,
do() {
console.log(this.data);
}
};
// export
export default data;
- line 9: we export the object [data];
Whether or not the exported object is named does not affect the import operation. The [import-02] script is as follows:
'use strict';
// import a default exported object
import module1 from './export-02';
// using this object
module1.do();
// a default export can be imported under any name
import module2 from './export-02';
console.log(module2.data);
Execution
[Running] C:\myprograms\laragon-lite\bin\nodejs\node-v10\node.exe -r esm "c:\Data\st-2019\dev\es6\javascript\modules\import-02.js"
2
2
10.3. scripts [import-03, export-03]
A module can export multiple elements.
The [export-03] script is as follows:
// multi-exports
// object export
const data = {
data: 2,
do() {
console.log(this.data);
}
};
// export function
export { data };
function doSomething() {
console.log("doSomething");
}
export { doSomething };
Comments
- lines 10 and 14: export of two elements. An element is exported using the syntax [export {élément}];
The script [import-03] uses the module [export-03] as follows:
'use strict';
// import a module [export03]
import {data, doSomething} from './export-03';
// using imports
data.do();
doSomething();
// other writing
import * as module from './export-03';
// using import
console.log(module.data);
module.doSomething();
- line 3: [imports] are created using the exact names of the exported objects;
- line 8: you can import all exported objects into a named object (here [module]);
Execution
[Running] C:\myprograms\laragon-lite\bin\nodejs\node-v10\node.exe -r esm "c:\Data\st-2019\dev\es6\javascript\modules\import-03.js"
2
doSomething
{ data: 2, do: [Function: do] }
doSomething