In the previous segment, you learnt that exports keyword is a shortcut to using module.exports.
Now in this segment, you will understand this better with the help of a code.
The code which you looked in the previous video is given below:
circle Module (Method 1):
const PI = 3.14; const calculateArea = r => PI * r * r; const calculateCircumference = r => 2 * PI * r; // using individual exports statement to export each construct one by one exports.calculateArea = calculateArea; exports.calculateCircumference = calculateCircumference;
circle Module (Method 2):
const PI = 3.14; // using exports statement while defining a function exports.calculateArea = r => PI * r * r; exports.calculateCircumference = r => 2 * PI * r;
app Module:
const circle = require('./circle.js'); const area = circle.calculateArea(8); const circumference = circle.calculateCircumference(8); console.log(`Area = ${area}, Circumference = ${circumference}`);
Output:
Area = 200.96, Circumference = 50.24
In the following video, you will use the last method which you have learnt in order to export constructs and see whether the output is as expected or not.
The code which you looked in the previous video is given below:
circle Module (Method 3):
const PI = 3.14; const calculateArea = r => PI * r * r; const calculateCircumference = r => 2 * PI * r; // using single exports statement to export all constructs at once exports = { calculateArea: calculateArea, calculateCircumference: calculateCircumference } // fixing the reference by assigning object inside exports to module.exports module.exports = exports;
app Module:
const circle = require('./circle.js'); const area = circle.calculateArea(8); const circumference = circle.calculateCircumference(8); console.log(`Area = ${area}, Circumference = ${circumference}`);
Output:
Area = 200.96, Circumference = 50.24