Modular pattern in javascript

Vedant Jain
1 min readNov 22, 2020

Modular pattern is a very popular code architecture in practice. As the name suggests this architecture brings modularity in the code.

we define a const variable module which is a IIFE (Immediately Invoked Function Expression). Haven’t we defined it in IIFE then we had to call the function something like this

module().somePublicMethod();

Now talking about the methods inside our module, we have defined somePrivateMethod as a private method of our module which can be called inside our module to perform some internal computations. This function will not be available outside our module and can’t be called directly via module variable.

module.somePrivateMethod() // throws Error

On the next part we have returned an object that contains key and value pair. The key is the name of the calling function and value is function to be executed. These functions are available publicly to be called via the module variable. Also, these functions can use all of our internal private function to compute some results.

--

--