Saturday 26 March 2022

JavaScript | Importing and Exporting Modules one .js file as library another is consumer of library -

 How to call the .js libraries in React or jqeury, the same way our custom js library behaving js modules.

JavaScript | Importing and Exporting Modules one .js file as library another is consumer of library -:

-----------------------------------------------------------------------------------------------------

library.js

-----------

<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>

Script.js

----------

<script>

// Importing the module library containing

// area and perimeter functions.

// " ./ " is used if both the files are in the same folder.

const lib =  require('./library');

  

let length = 10;

let breadth = 5;  

// Calling the functions 

// defined in the lib module

lib.area(length, breadth);

lib.perimeter(length, breadth);

</script>

Output:

Area of the rectangle is 50 square unit

Perimeter of the rectangle is 30 unit

--------------------------------------------------------------------------------------------------------

JavaScript Modules are basically libraries which are included in the given program. They are used for connecting two JavaScript programs together to call the functions written in one program without writing the body of the functions itself in another program.


Importing a library: It means 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 relative path.

Example: Suppose 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') 


which will return reference to that library. Now if there is area function defined in the library, then use it as lib.area().


Exporting a library: There is a special object in JavaScript called module.exports. When some program include or import this module (program), this object will be exposed. Therefore, all those functions that need to be exposed or need to be available so that it can used in some other file, defined in module.exports.


Expample : Write two different programs and then see how to use functions defined in the library (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 breadth. Then export the functions so that other programs can import them if needed and can use them.


library.js

-----------

<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>


For importing any module, use a function called ‘Require’ which takes in the module name and if its user defined module then its relative path as argument and returns its reference.


The script.js contains the above JavaScript module (library.js).

Script.js

----------

<script>

// Importing the module library containing

// area and perimeter functions.

// " ./ " is used if both the files are in the same folder.

const lib =  require('./library');

  

let length = 10;

let breadth = 5;

  

// Calling the functions 

// defined in the lib module

lib.area(length, breadth);

lib.perimeter(length, breadth);

</script>

Output:

Area of the rectangle is 50 square unit

Perimeter of the rectangle is 30 unit


No comments:

Post a Comment