Export Import Prog
Export Import Prog
any object, class, literal, or function of one JavaScript file to any other JavaScript file. It
improves the code’s reusability and reduces the loading time of the HTML file. For this purpose,
the JavaScript modules provide two keywords, “import” and “export”Modules in JavaScript
use the import and export keywords:
simply write the “export” keyword prior to that variable, function, class, or anything we want to
export.
//exporting a variable
export var emp_name;
//exporting a function
export function emp(){
}
//exporting a class
export class employee{ }
//importing a function
//importing a class
document.write("Hello World!");
constructor(name) {
Example 1: Create a file named import.html and write the below code in that file.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Import in ES6</title>
</head>
<body>
<script type="module">
// Default member first
document.write (num_set);
hello();
document.write (g.greeting);
</script>
</body>
</html>
Example 2:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Import in ES6</title>
</head>
<body>
<script type="module">
document.write (exp.num_set);
exp.hello();
document.write (g.greeting);
</script>
</body>
</html>
Example
var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
document.write(person.lastName);
Example:
//document.write(delete emp.salary);
document.write(emp.salary);
</script>