Exporting a Class or Object
Learn how to export a class or object in Node.js.
We'll cover the following...
Numerous modules in the Node.js ecosystem export only a single object aggregating all of the module’s functionality. To do so, they reassign the module.exports
object instead of adding properties to it. For example, check out how the following module calculator.js
is defined.
Press + to interact
// Declare a factory function that returns an object literalconst createCalc = () => {// The returned object has 4 methodsreturn {add(x, y) {return x + y;},substract(x, y) {return x - y;},multiply(x, y) {return x * y;},divide(x, y) {return x / y;}};};// Export the factory functionmodule.exports = createCalc;
In this ...