Last active
April 20, 2017 16:08
-
-
Save joepie91/83a8e03ad931e696df22 to your computer and use it in GitHub Desktop.
Configurable module using closures in Node.js / CommonJS (ie. parametric modules)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(config) { | |
return function actualFunctionality(someArgument){ | |
return doSomethingWith(config).and(someArgument); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(config) { | |
return { | |
functionalityOne: function(someArgument) { | |
return doSomethingWith(config).and(someArgument); | |
}, | |
functionalityTwo: function(someArgument) { | |
return doSomethingElseWith(config).and(someArgument); | |
} | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = loadConfiguration("config.js"); | |
var someModule = require("./some-module")(config); | |
var someObjectModule = require("./some-object-module")(config); | |
someModule("cake"); | |
someObjectModule.functionalityOne("cake"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you provide an example of what might be in
config.js
?