DEV Community

Awais Akram Mughal
Awais Akram Mughal

Posted on

Revealing Module Design Pattern

Defining all functions and variables in private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.

var myRevealingModule = (function() {
  var privateVar = "Awais Mughal";
  function privateFunction() {
    console.log('name: ', privateVar);
  }

  function publicGetName() {
    privateFunction();
  }

  return {
    getName: publicGetName
  }
})()

myRevealingModule.getName(); // * name: Awais Mughal

Enter fullscreen mode Exit fullscreen mode

How you can improve the readability in case of multiple functions?

  • Move the return object to the top, so when you look at the code days after writing, it'll be easy to know the public methods.

  • Focus on Naming conventions

Advantages:

  • Syntax of our Script will be more consistent.
  • readability
  • Accessibility Concerns are handled

Disadvantages:

  • If a private function refers to the public function, that public function can't be overridden wile using it on other places(calling the public function from another file) if a patch is necessary. WHY? Because you can't manipulate the implementation of private function from outside.

  • Public Object members which refer to private variables are also subject to the no-patch rule note above.

Top comments (0)