In javascript is there a name for this idiom / pattern? A function which has about 10 inner functions and 10 vars, and returns an object literal usually name “that” which returns a a subselection of the inner functions and vars. For example:
function myFunction() {
var myVar1;
var myVar2;
var myVar3;
...
...
function myInnerFunction1() {
...
}
function myInnerFunction2() {
...
}
function myInnerFunction3() {
}
var that = {
inner1: myInnerFunction1,
inner2: myInnerFunction2,
var1: myVar1
}
return that;
}
It’s called “the module pattern” and/or “creating a namespace.” It’s so you have a private scope for your stuff (the execution context of the call to the wrapper function,
myFunctionin your example), and you return an object that only has the things on it that you want to make publicly accessible. Those things (myInnerFunction1for example) have access to the private information within the wrapper function, but nothing using the resulting object does.In the module pattern, you may well not export anything, if you have nothing public you need to expose directly (for instance, your code is completely self-contained, setting up event handlers and such).