JavaScript | Importing and Exporting Modules
JavaScript Modules are basically libraries which are included in the bestowed program. They are used for connecting two JavaScript programs simultaneously to call the functions written in one program without writing the body of the functions itself in another program.
Import library: It proportion include a library in a program so that use the function is defined in that library. For this, use ‘require’ function in which pass the library name with its correlative way.
Example: envision a library is made in the same folder with file name library.js, then include the file by using require function:
Example : Write two various programs and then glance how to use functions defined in the Module in given program. Define two simple functions in the library for calculating and printing area and perimeter of a rectangle when provided with length and width. Then export the functions so that other programs can onrush them .
<script>
// Area function
let area = function (length, breadth) {
let a = length * breadth;
console.log('Area of the rectangle is ' + a + ' square unit');
}
// Perimeter function
let perimeter = function (length, breadth) {
let p = 2 * (length + breadth);
console.log('Perimeter of the rectangle is ' + p + ' unit');
}
// Making all functions available in this
// module to exports that we have made
// so that we can import this module and
// use these functions whenever we want.
module.exports = {
area,
perimeter
}
</script>
Output:
Note: To run the script 1st create both files in the identical folder and then run script.js using NodeJs interpreter in terminal.
JavaScript | Object Methods
Introduction to Object Oriented Programming in JavaScript
JavaScript :The awing Scrip
JavaScript Modules are basically libraries which are included in the bestowed program. They are used for connecting two JavaScript programs simultaneously to call the functions written in one program without writing the body of the functions itself in another program.
Import library: It proportion include a library in a program so that use the function is defined in that library. For this, use ‘require’ function in which pass the library name with its correlative way.
Example: envision a library is made in the same folder with file name library.js, then include the file by using require function:
const lib = require('./library'
Exporting a library: There is a particular object in JavaScript called module.exports. When some program include or import this program, this object will be manifested. consequently, all those functions that requirement to be exposed or need to be available so that it can used in some other file, defined in module.exports.Example : Write two various programs and then glance how to use functions defined in the Module in given program. Define two simple functions in the library for calculating and printing area and perimeter of a rectangle when provided with length and width. Then export the functions so that other programs can onrush them .
<script>
// Area function
let area = function (length, breadth) {
let a = length * breadth;
console.log('Area of the rectangle is ' + a + ' square unit');
}
// Perimeter function
let perimeter = function (length, breadth) {
let p = 2 * (length + breadth);
console.log('Perimeter of the rectangle is ' + p + ' unit');
}
// Making all functions available in this
// module to exports that we have made
// so that we can import this module and
// use these functions whenever we want.
module.exports = {
area,
perimeter
}
</script>
Note: To run the script 1st create both files in the identical folder and then run script.js using NodeJs interpreter in terminal.
JavaScript | Object Methods
Introduction to Object Oriented Programming in JavaScript
JavaScript :The awing Scrip