This is the Revealing Module Pattern.
The returned object contains references to the functions defined inside the IIFE. So the functions defined inside are private to the anonymous function.
But if you want to use the inner functions outside, you can use the returned object.
The value of Test
will be
var Test = {
func1: func1,
func2: func2,
func3: func3
};
And you can call func1
from outside as
Test.func1();
This is the way Javascript emulate class. As there is no visibility specifiers using Module pattern, variables/methods can be make public/private.
The revealing module pattern is inspired from Module pattern. In revealing module pattern, only reference to the private variables/methods is returned in an object.
The main idea behind the pattern is avoiding evil global variables. This looks similar to IIFE except an object is returned instead of function. The variables/methods defined inside the IIFE are private to the function. To access any variable/method inside the IIFE, it needs to be added in the returned object and then it can be accessed from outside of IIFE. This pattern takes advantage of closures, so the variables/methods defined inside the IIFE are accessible even after the object is returned.
From Addy Osmani's book Learning Javascript Design patterns
The Revealing Module pattern came about as Heilmann was frustrated with the fact that he had to repeat the name of the main object when we wanted to call one public method from another or access public variables. He also disliked the Module pattern’s requirement for having to switch to object literal notation for the things he wished to make public.
The result of his efforts was an updated pattern where we would simply define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.
Advantages:
- Encapsulation. The code inside the IIFE is encapsulated from outside world
- Clean, organized and reusable code
- Privacy. It allows to create private variables/methods. The private variables/methods cannot be touched from outside of the IIFE.
Disadvantages:
- If a private function refers to a public function, that public function can't be overridden
Further Reading:
- https://en.wikipedia.org/wiki/Module_pattern
- https://carldanley.com/js-revealing-module-pattern/
- How to use Revealing module pattern in JavaScript
EDIT
From comment from @Mike
It's of note that it's common to create an object (eg, var me = {};
) and then declare the would-be public members on it (me.func1 = function() { /* ... */ };
), returning that object at the end (return me;
). This avoids the repetition that we see in the return statement of OP's code (where all the public stuff is repeated).
var me = {};
) and then declare the would-be public members on it (me.func1 = function() { /* ... */ };
), returning that object at the end (return me;
). This avoids the repetition that we see in the return statement of OP's code (where all the public stuff is repeated). – Cutin