Module Pattern: Essential JavaScript Concepts

1 minute read

The Module Pattern is one of the most common design patterns used in JavaScript. It is easy to use and creates encapsulation of our code. Before we deep dive into this, first let’s see

  • Where do we need this module pattern?
  • What is its significance?

In singleton design pattern, let’s say we have requirement like we have very less sources and those sources we do not want to keep creating multiple times. We just want to have only one instance throughout the app, in those scenarios we do singleton design pattern on server side. We make sure whenever somebody ask for instance it has to call some method in the object.

For example on the server side if we have class called logger and whoever wants instance of this logger then they have to call get instance method in this class and it has to be a static method. This is exactly what we want in JavaScript and in this video tutorial JavaScript Concepts- Module Pattern | 0004-11 we will see how we achieve it in JS. When we do not have static concept and we cannot create a static function. This is where the concept of Module pattern comes.

When we need a single instance of an object throughout our app we do not need to create a class and then instantiate it, instead we can use Module pattern. It has many benefits over other pattern types like regular JS class and prototype Pattern. In our video tutorial we will see how we can create a class with private function. This video will answer most of the important question like

  • Why module pattern is helpful for creating singleton objects in JavaScript?
  • Can we write static function in a class?
  • Can we get private function in JavaScript or not?

Please visit our video tutorial to get your answers, snapshot of session below: