JavaScript Pattern: Revealing Module April 30th, 2013

This pattern is essentially the same as the Module Pattern, except all of the functions and variables are defined in the private scope, and we return an object with pointers to the private functionality that we wish to make public.

var myRevealingModule = function () {

        var privateCounter = 0;

        function privateFunction() {
            privateCounter++;
        }

        function publicFunction() {
            publicIncrement();
        }

        function publicIncrement() {
            privateFunction();
        }

        function publicGetCount(){
          return privateCounter;
        }

        // Reveal public pointers to 
        // private functions and properties        

       return {
            start: publicFunction,
            increment: publicIncrement,
            count: publicGetCount
        };

    }();

The main advantage to this approach is organization and readability. With this approach, we can also define a more specific naming scheme for the module for public methods.

myRevealingModule.start();

Resources