DEV Community

Nevin Katz
Nevin Katz

Posted on

1 1

Exploring Javascript's Module Design Pattern

I remember that a few years back, I found it pretty hard to wrap my head around closures in Javascript. After months of struggling with them with readings, exercises, and tutorials, what helped me get a handle on them was using them in my day-to-day work. In my web and app development, I started writing immediately-invoking function expressions that used closures. My initial expressions simply had a getter and a setter, as in the function below:


var myFunc = (function() {
   let num = 0;
   return {
        get:function() {
          return num;
        },
        set:function(x) {
          num = x;
        }
   };
})();

Enter fullscreen mode Exit fullscreen mode

And over time I ended up building out more complex expressions.

I later found out that I was routinely using what is known as the module design pattern. As I continued to built out variations of this pattern, I found that it became quite useful in compartmentalizing the numerous components in web interactives and in my Cordova app, which now has a fairly large codebase.

Last night I started thinking how I might teach the module design pattern to someone relatively new to Javascript. I wrote a quick tutorial on the pattern, which is now available in CodeX. Feel free to give it a read - I hope you find it helpful.

https://medium.com/codex/put-the-javascript-module-design-pattern-to-work-41396c577084

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
gabrielrufino profile image
Gabriel Rufino

Nice!

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