When working with lots of classes in a node module is the following mechanism a good idea or is there a better way to achieve this?
// mymodule/core.js
module.exports = {
ClassA: require('./class/ClassA'),
ClassB: require('./class/ClassB'),
ClassC: require('./class/ClassC')
}
// mymodule/class/ClassA.js
module.exports = function() {
...
}
// myapp.js
var core = require('mymodule/core')
;
var a = new core.ClassA();
The idea of the above is to keep classes in physically separate modules for maintenance whilst providing a namespace that is easy to use.
For me it is far clearer to have the modules you are using pulled in within the module using them. You can’t get around the fact that the module has these dependencies and it will be easier for someone else reading the code to see what is being used.
Also if you ever wanted to reuse a single module you would have to also carry with it the
core.jsmodule–and all of it’s dependencies–from which your module is coupled.