Java Script Modules (1)
Java Script Modules (1)
Modules are the piece or chunk of a JavaScript code written in a file. JavaScript
modules help us to modularize the code simply by partitioning the entire code into
modules that can be imported from anywhere. Modules make it easy to maintain the
code, debug the code, and reuse the piece of code. Each module is a piece of code that
gets executed once it is loaded.
During the build process, we can use Gulp, Babel, Grunt, or other transpilers for
compiling the modules. The variables and functions in a module are not available for
use unless the file exports them.
Exporting a Module
o Named Exports: The exports that are distinguished with their names are
called as named exports. We can export multiple variables and functions by
using the named export.
o Default Exports: There can be, at most, one value that can be exported by
using the default export.
Importing a Module
To import a module, we need to use the import keyword. The values which are
exported from the module can be imported by using the import keyword. We can
import the exported variables, functions, and classes in another module. To import a
module, we simply have to specify their path.
When you import the named export, you must have to use the same name as the
corresponding object. When you import the default export, we can use any name of
the corresponding object.
Syntax
Let us see the syntax to use named export in class, function, or in a variable. Below
we are showing how to individually export the class, variable, and function by using
the named export.
We can apply more than one named export in a module. We can use the syntax of
multiple named exports in a module as follows:
Mobile.js
1. class Nokia{
2. //properties
3. //methods
4. }
5. function show(){
6. }
7. const a = 10;
8. export {Nokia, show};
To import the bindings that are exported by another module, we have to use the
static import statement. The imported modules are always in the strict mode,
whether we declare them in strict mode or not.
Syntax
App.js
Importing all
If we want to import all of the export statements simultaneously, then we can import
them individually.
But it will be hard when we have so many named exports. So, to make it easier, we
can do it as follows:
1. import * as device from './Mobile.js'; // Here, the device is an alias, and Mobile.js is t
he module name.
Suppose, we have defined a class Nokia in the module Mobile.js, and if we want to
use it then by using the alias, we can perform it as:
Let us try to understand the Named export and import within a single example.
We have to make two JavaScript modules to perform export and import. In the first
module, we export the class, function, and variable. In the second module, we will
import them by using the named import.
Here, we are creating two JavaScript modules in which the first JavaScript module
is Mobile.js, and the second module name is App.js. We are also creating
an HTML file Example.html. Then, we will execute this .html file in the server.
Next, we have to manually link the JavaScript file in the HTML file by using the src
attribute in the <script></script> tag. But the module still will not work. To enable
the module, we have to use the type = "module" in the <script></script> tag.
Example.html
1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
7. <script type = "module" src = "App.js"></script>
8. <title>Document</title>
9. </head>
10. <body>
11. <h1>ES6 Modules import and export</h1>
12. </body>
13. </html>
Mobile.js
1. class Display{
2. show(){
3. console.log("Hello World :)");
4. }
5. }
6. function javaT(){
7. console.log("Welcome to javaTpoint");
8. }
9. export {Display,javaT};
App.js
Output
Run the above Example.html file in the live server. Then, open the terminal in the
browser by using ctrl+shift+I. After the successful execution, you will get the
following output:
Default Exports and Imports
We can have only one default export in a module. A default export can be imported
with any name.
Syntax
Let us see the syntax to use default export in class, function, or in a variable. Below
we are showing how to individually export the class, variable, and function by using
the default export.
ADVERTISEMENT
ADVERTISEMENT
Syntax
Mobile.js
1. class Nokia{
2. //properties
3. //methods
4. }
5. export default Nokia;
App.js
Example.html
1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
7. <script type = "module" src = "App.js"></script>
8. <title>Document</title>
9. </head>
10. <body>
11. <h1>ES6 Modules import and export</h1>
12. <h2>Default import and export</h2>
13. </body>
14. </html>
Mobile.js
1. class Display{
2. show(){
3. console.log("Hello World :)");
4. console.log("Default Import and Export Example");
5. }
6. }
7. export default Display;
App.js
Output
Run the above Example.html file in the live server. Then, open the terminal in the
browser by using ctrl+shift+I. After the successful execution, you will get the
following output:
Cyclic dependency is a direct or indirect relation between two or more modules that
depend on each other. Such modules are known as the mutually recursive modules.
The modules in ES6 automatically supports the cyclic dependencies. Two modules,
such as A and B, are cyclically dependent on each other if both A imports B and B
imports A either transitively or indirectly.
CommonJS and some of the other libraries support the circular dependencies, but
there is a problem with importing and using named exports from a circular
dependent module.
ES6 solves this problem by sharing the bindings to the values rather than the values
itself in the exports. It means the connection with the variables declared in the body of
the module can be done. We can see the demonstration for the same in the following
code:
Example
Example.html
1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
7. <script type = "module" src = "App.js"></script>
8. <title>Document</title>
9. </head>
10. <body>
11. <h1>ES6 Cyclic dependencies</h1>
12. </body>
13. </html>
Mobile.js
App.js
Output