DEV Community

Awais Akram Mughal
Awais Akram Mughal

Posted on

1 4

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay