Module in ES6

Facebooktwitterredditpinterestlinkedinmailby feather

Module in ES6

In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with the help of partitioning it into small separate pieces of code. These code pieces can be imported for other purposes.The modules also help us to manage the source code, reuse the code, and debug the code. TheModule can be used after it is loaded.

Module in JavaScript is an important concept. It is a new feature introduced in ES6. We can also use the concept of the module in ES5 with the help of the Transforming process. Transforming is a process used to change the code of one language into a similar language.

We can use the module transpiler tool to change the ES6 module into an ES5 module in JS style or AMD. The AMD (Asynchronous Module Definition) is used as a JavaScript specification.  We can also use Grunt, babel, and Gulp tool to perform module transpilation.

Exporting a Module

ES6 helps us to export the primitive value, object, function, and class with the help of the export keyword. The term export can be divided into following two types:

Default Export: It helps us to export one or more than one value with the help of the export keyword.

Named Export: The export that can be identified by its name is known as a Named Export. It helps us to export the functions and multiple variables with the help of the export keyword.

Importing a Module

We can import a module with the help of the import keyword. It helps us to import the value which is exported from a module. The import keyword is also used to import function, variable, and class from one to another module. If we want to import a module, we need to define the path of modules.

If we import named export, then we need to use the same name for the module object.

If we import the default export, then we can use different names for the module object.

Now, we will discuss the imports and exports with their syntax and examples.

Named Export and Imports

The Named exports are differentiated by their names. We can import the function, class, and variable (exported by the named export) with the same name. We can also import and export multiple functions by using the named export.

Here, we have the syntax to see how we can export the function, class, and variable by using the named export.

Syntax

A module can contain more than one Named export. We can also work with multiple named export with the help of the following syntax.

Importing the Named Export

We can import the properties of a module exported by other modules with the help of a static import statement. The imported modules always lie inside the strict mode, whether they are declared or not in the same mode.        

Syntax

Application.js

We can import all export statements by importing them separately. But it is complex to perform if we have more than one named export. We can perform it as given below.

Now, we will define a class Samsung inside the module Mobile.js. We can perform it as given below when we try to access it with the help of an alias.

Default Exports and Imports

A module only contains a single default export. We can import a default export in a module by using any name.The default export is used to export variable, class, and function. We can export only one export at a time in default export.

Here, we have the syntax of default export in variable, class, and function.

Syntax

Importing the Default Export

We can import the properties of a module that are exported by other modules with the help of static export statements. The imported modules always lie inside the strict mode, whether they are declared or not in the same mode.  

Syntax
Mobile.js

Application,js

Example

Here, we will discuss the default export with the help of the following example.

Mobile.js

Application.js

Output

Now, we execute the Sample.html file with the help of server, and press Ctrl+Shift+I to open the terminal. After the execution of the code, we got the following output:

Module in ES6

Cyclic Dependency in ES6

The term cyclic dependency refers to as a relation between two modules that confide on each other. These modules also can be called as recursive modules. In ES6, the modules support the cyclic dependency.

The two modules P and Q is said to be cyclic dependent if P imports Q, and Q imports P in direct or indirect manner.

For Example: Suppose we have three modules P, Q, and R. Their dependency chain is like-

P>Q       P depends upon Q.  

Q>R      Q depends upon R.

R>P       R depends upon P.

JS library and many other libraries supports the cyclic dependency, but many times the user face some problems during the importing and using named export from a cyclic dependent module.  ES6 provide a solution to solve this problem. ES6 wrap the value instead of value itself in the export. It means we can make connectivity with the variables declared inside the module body.

Here, we have an example to understand the above concept.

Example

Mobile.js

Application.js

Output

Now, we execute the html file with the help of server, and press Ctrl+Shift+I to open the terminal. After the execution of the code, we got the following output:

Module in ES6
Facebooktwitterredditpinterestlinkedinmailby feather