Hi everyone!
I am reading the article in the javascript modules chapter: https://www.codecademy.com/courses/learn-intermediate-javascript/articles/implementing-modules-in-node
and I am struggling to understand the "Using object destructuring to be more selective with require() part.
The article says that the code snippet extracts the celsiusToFahrenheit() method and leaves fahrenheitToCelsius() behind.
/* celsius-to-fahrenheit.js */
const { celsiusToFahrenheit } = require('./converters.js');
const celsiusInput = process.argv[2];
const fahrenheitValue = celsiusToFahrenheit(celsiusInput);
console.log(`${celsiusInput} degrees Celsius = ${fahrenheitValue} degrees Fahrenheit`);
So it seems like the require() part is importing the converters.js, which is containing celsiusToFahrenheit function and fahrenheitToCelsius function. How does assigning this to const ( celsiusToFahrenheit } result in extracting celsiusToFahrenheit()?
/* converters.js */
function celsiusToFahrenheit(celsius) {
return celsius * (9/5) + 32;
}
module.exports.celsiusToFahrenheit = celsiusToFahrenheit;
module.exports.fahrenheitToCelsius = function(fahrenheit) {
return (fahrenheit - 32) * (5/9);
};
This is the converters.js code just in case the link doesn’t work.