javascriptdesign-patternsrevealing-module-pattern

Passing arguments into the revealing Module pattern in Javascript


How can you pass arguments to a function when implementing the Revealing module pattern in JavaScript. I have a module like this

var GlobalModule = (function() {
    function consoleLog(textToOutput) {
        console.log(textToOutput);
    }

    return {
        consoleLog: consoleLog()
    };
})();

Later on, when i run GlobalModule.consoleLog("Dummy text");, I get undefined as the output.


Solution

  • Do with function inside the return object

    var GlobalModule = (function() {
      return {
        consoleLog: function(textToOutput)
        {
         console.log(textToOutput);
        }
      }
    })();
    
    GlobalModule.consoleLog("Dummy text");

    Simply Do like this same output achieved . object => function call .No need a return object

     var GlobalModule ={
            consoleLog: function(textToOutput) {
                                        console.log(textToOutput);
                                      }
          }
    
        GlobalModule.consoleLog("Dummy text");