Es6 Module

codingzon-es6-Modules

Export-Import in ES6

JavaScript's es6 allow you to organize the code components in the form of modules and can be import them into other modules.

This makes code maintenance easier. If they are written in separate files.

With the help of import and export keywords, Modules can be used in other programs.

Using Export


You can export a function or variable from any file.

Let us create a file named employee.js, and fill it with the things we want to export.

There are two types of exports: 1. Named and 2. Default.

Named exports must be enclosed using curly braces. Default exports do not.

Named Exports

You can create named exports two ways. 1. In-line individually, or 2. All at once at the bottom.

MOVETOP↑

 

Example
In-line individually:

export const empname = "benny"
export const empage = 30

// first declare variable and export
let empcompany = "xyz"
export{empcompany }




All at once at the bottom:

employeerecord.js

// first declare variable and export

const empname = "sunny"
const empage = 30
const empcompany = "xyz"
 
export{empname,empage,empcompany }


Default Exports

Only one default export can be used in a file.In the given example, there is only one export.

Example
empdetails.js

const getEmpDetails = () => {
  const empname = "Jenny";
  const empage = 30;
  return empname  + ' ,' + empage ;
};


export { getEmpDetails}

 

Using Import


You can import modules into a file in two ways, either it can name or default export.


Example
Import named exports from the file employeerecord.js: in the below given file employeerecord2.js

1. Import named exports


employeerecord2.js
 
import {empname, empage, empcompany }  './employeerecord.js'
 
 
console.log(empname);
console.log(empage);
console.log(empcompany);
 
 
 

namedimport.html

This HTML file will execute the employeerecord2.js component, which is embedded in the script tag. And must be specified type="module".

<!DOCTYPE html>
<html>
<head>

<title>ES6 Modules</title>
   
</head>
<body>
  <script src="./employeerecord2.js" type="module"> 
  
</script>

</body>
</html>

 TOP


Example:

empdetails.js

 let empcompany = "abc"

const getEmpDetails = () => {
  const empname = "Jenny";
  const empage = 30;
  return empname  + ',' + empage ;
};
let getCompany = function(){
   return empcompany.toUpperCase()
}

export {getEmpDetails, getCompany} //default export
  
 

 empdetails3.js

 import {getEmpDetails,getCompany}  from './empdetails.js'
 
console.log(getEmpDetails() );
console.log(getCompany() );
 

GOTOP↑

 


2. Import a default export

 empdetails2.js

import  getEmpDetails a from "./empdetails.js";

 console.log(getEmpDetails);

 

Another approach.. 
 

The component can be renamed while importing as shown in the example 

empdetails4.js

import  * as emp from "./empdetails.js";

 console.log(emp.getEmpDetails);



defaultimport.html

<!DOCTYPE html>
<html>
<head>

<title>ES6 Modules</title>
   
</head>
<body>
  <script src="./empdetails4.js" type="module"> 
  
</script>

</body>
</html>

 


 ES6HOME

 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP