I am working to emulate the require functionality with a few javascript functions, for example:
// We set up an object that will include all the public modules
// And then a require function which basically just accesses that public module
const modules = {};
function require(moduleName) {
return modules[moduleName];
}
modules['math.js'] = function() {
// hidden implementation details
let incr = x => ++x;
return {
incr,
sum(x,y) { return x+y; }
}
}
Why is it required to do modules['math.js'] = (function() {...})(); and not just a normal non-invoked function call as above?