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)

Classic DEV Post from 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!