Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active April 20, 2017 16:08
Show Gist options
  • Save joepie91/83a8e03ad931e696df22 to your computer and use it in GitHub Desktop.
Save joepie91/83a8e03ad931e696df22 to your computer and use it in GitHub Desktop.
Configurable module using closures in Node.js / CommonJS (ie. parametric modules)
module.exports = function(config) {
return function actualFunctionality(someArgument){
return doSomethingWith(config).and(someArgument);
};
}
module.exports = function(config) {
return {
functionalityOne: function(someArgument) {
return doSomethingWith(config).and(someArgument);
},
functionalityTwo: function(someArgument) {
return doSomethingElseWith(config).and(someArgument);
}
};
}
var config = loadConfiguration("config.js");
var someModule = require("./some-module")(config);
var someObjectModule = require("./some-object-module")(config);
someModule("cake");
someObjectModule.functionalityOne("cake");
@sparr
Copy link

sparr commented Mar 11, 2017

Can you provide an example of what might be in config.js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment