1

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript

I was going through the above master piece. I am still not able to get two things from the above article.

var myRevealingModule = (function () {

        var privateVar = "Ben Cherry",
            publicVar  = "Hey there!";

        function privateFunction() {
            console.log( "Name:" + privateVar );
        }

        function publicSetName( strName ) {
            privateVar = strName;
        }

        function publicGetName() {
            privateFunction();
        }


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

        return {
            setName: publicSetName,
            greeting: publicVar,
            getName: publicGetName
        };

    })();

myRevealingModule.setName( "Paul Kinlan" );

Related question: JavaScript design pattern: difference between module pattern and revealing module pattern?

I understood many parts from the above question. Can anyone convert the above revealing module pattern into a traditional module pattern, so that i can visualize the difference.

Community
  • 1
  • 1
theJava
  • 14,620
  • 45
  • 131
  • 172
  • As far as I can tell, the `revealing module pattern` is just another way of doing `module pattern`. If you want another way of doing it, look at `node.js` modules (`commonjs`), or `amd` modules. That's sort of more just `modules` (in my mind at lest), cause there aren't any explicit closures. Though, in the end, it's basically just the same, it's just some of the code is generated for you. – Alxandr May 14 '14 at 09:04
  • Also, a point about the code you posted above. `publicVar` isn't public, because `myRevealingModule.greeting` isn't a pointer, as proven here: http://jsfiddle.net/Xr2kB/ – Alxandr May 14 '14 at 09:10

2 Answers2

0

The module pattern code looks like the following: You can see the jsfiddle

var myModule = (function () {

    var privateVar = "Ben Cherry";

    function privateFunction() {
        console.log( "Name:" + privateVar );
    }

    return {
        setName: function( strName ) {
            privateVar = strName;
        },
        greeting: "Hey there!",
        getName: function() {
            privateFunction();
        }
    };

})();
myModule.setName( "Paul Kinlan" );
Vinoth
  • 657
  • 6
  • 12
0

You have two options when avoiding the Revealing Module Pattern. You can either use a object literal or a stub return object.

Using an object literal:

var myRevealingModule = (function () {
   var privateVar = "Ben Cherry";

   function privateFunction() {
      console.log( "Name:" + privateVar );
   }

   return {
      setName: function( strName){
         privateVar = strName;
      },
      greeting: "Hey there!",
      getName: function() {
         privateFunction();
      }
   };

})();

Using a stub:

var myRevealingModule = (function () {
   var privateVar = "Ben Cherry";
   function privateFunction() {
       console.log( "Name:" + privateVar );
   }

   var stub = {};
   stub.greeting = "Hey there!";
   stub.setName = function ( strName ) {
      privateVar = strName;
   };
   stub.getName = function () {
      privateFunction();
   }
   return stub;

})();

The key point is to not put anything in the closure unless you intend to hide it. Don't put something in the closure and then "reveal" it later.

I-Lin Kuo
  • 3,220
  • 2
  • 18
  • 25