A JavaScript module is a reusable piece of code that can be exported from one file and imported into another. This helps to organize and manage code more efficiently, especially in large applications.
Why Do We Need Modules?
1. Modules allow you to break your code into smaller, manageable parts. This makes it easier to work on, test, and maintain.
2. Code in a module can be reused across different files or even projects.
3. Variables and functions in a module are not visible to the global scope, so you can avoid polluting the global namespace and creating conflicts between different pieces of code.
4. Large applications are easier to maintain when code is divided into logical modules, making it simpler to locate and update specific functionality.
How to export functions, constants from a module?
In the context of Node.js, each file is treated as a module. If you want to make certain functions, constants, or objects available to other files (modules), you need to export them.
arithemetic-operations.js
function add(a, b){ return a + b; } function subtract(a, b){ return a - b; } const PI = 3.14; module.exports = {PI, add}
By using module.exports = { PI, add }, only the PI constant and the add function are being exported. The subtract function is not exported, so it remains private to the current module and cannot be used in other files unless explicitly exported.
How to import this module?
Using 'require' method, we can import a functions, constants from another module.
const {PI, add} = require('./arithemetic-operations');
Above statement declares two variables named PI and add. These variables will be assigned values from the imported module './arithemetic-operations'.
arithmetic-operations.js
function add(a, b){ return a + b; } function subtract(a, b){ return a - b; } const PI = 3.14; module.exports = {PI, add}
module-demo1.js
const {PI, add} = require('./arithemetic-operations'); console.log(`Sum of 10 and 20 is ${add(10, 20)}`); console.log(`Value of PI is ${PI}`); // console.log(`Subtract of 10 and 20 is ${subtract(10, 20)}`);
Output
Sum of 10 and 20 is 30 Value of PI is 3.14
Is the name of variables {PI, add} must be same as like the imported module?
No, the names of the variables in the destructuring assignment do not have to match the names of the properties in the imported module.
Example
const { PI: myPi, add: myAdd } = require('./arithemetic-operations');
In this case, the PI property from the imported module will be assigned to the variable myPi, and the add property will be assigned to the variable myAdd.
module-demo2.js
const { PI: myPi, add: myAdd } = require('./arithemetic-operations'); console.log(`Sum of 10 and 20 is ${myAdd(10, 20)}`); console.log(`Value of PI is ${myPi}`); // console.log(`Subtract of 10 and 20 is ${subtract(10, 20)}`);
Output
Sum of 10 and 20 is 30 Value of PI is 3.14
How to import all the exported properties and functions?
const { ...arithUtil } = require('./arithemetic-operations');
This code will import all the exported properties and functions from the arithemetic-operations module and assign them to the arithUtil object. You can then access these properties and functions using dot notation.
For example, if the arithemetic-operations module exports PI and add methods, you can access them like below.
arithUtil.PI arithUtil.add(2, 3)
This approach is especially useful when you need to import a large number of properties or functions from a module.
module-demo3.js
const { ...arithUtil } = require('./arithemetic-operations'); console.log(`Value of PI is ${arithUtil.PI}`); console.log(`Sum of 2 and 3 is ${arithUtil.add(2, 3)}`);
Output
Value of PI is 3.14 Sum of 2 and 3 is 5
No comments:
Post a Comment