Nodejs
From John Freier
Revision as of 12:25, 5 January 2023 by Jfreier (Talk | contribs) (Created page with "== How to use Require == Require will cache objects after it calls them, kind of like a singleton pattern. To build out an object that require can grab there are a couple of...")
How to use Require
Require will cache objects after it calls them, kind of like a singleton pattern.
To build out an object that require can grab there are a couple of different ways.
The following way will create an object based on a function so it can have private variables.
example.js
const exampleService = (function() {
const self = this;
self.print = function() {
console.log('...');
}
return this;
})();
module.exports = exampleService;
Another way is to create a kind of JSON object with functions.
example.js
const exampleService = {
name: 'example',
print: function() {
console.log('...');
}
};
module.exports = exampleService;