I have a massive function that I would like to decompose into multiple files. Here is the structure of my function:
var myFunction = function (argument) {
var private1, private2, private3;
var gadget1 = (function () {
// Play around with private1, private2, private3, argument
return gadget1API;
}());
var gadget2 = (function () {
// Play around with private1, private2, private3, argument
return gadget2API;
}());
var gadget3 = (function () {
// Play around with private1, private2, private3, argument
return gadget3API;
}());
return myFunctionAPI;
}
Here namespacing the gadgets to spread them over multiple files doesn’t work because they all depend upon private1, private2, private3 which are within the myFunction closure. For this to work, I would need the myFunction closure to close over 3 different files.
You should create it as an object, not a closure, and load it first, something like this:
And instantiate it right after you defined it
you can access it’s functions like this: