1. Creating a Module
First we need to understand about “Immediately Invoked Function Expression (IIFE)”. These IIFEs are simple function when declared, are called immediately.

IIFE

This will also create a new scope where we can put all out logics. This is an anonymous module. We can namespace this module as

Simple Module

2. Creating a private method

Private method simply is a method that are wrapped inside a module, intended to prevent access from externally outside of the module. This will make our methods more secure. Private methods are useful when we have to access sensitive data over the internet or make external server calls.

code3

3. Module Gateway: “Return”

Inside the module, we will reveal only part of the scope for external access. This is made possible by using the return statement within the Module. This return statement return object the mode and is made accessible from the module’s namespace.

code4

We can access this public method as:

code5

4. Code management with “Locally scoped object literals”

We see in above code that we aren’t namespacing our return object. We can also namespace our returned object with the module scope that makes our code more managed and readable. We can also segregate our private methods from return within the namespaced object.

code6

5. Desing better with revealing module pattern

With revealing module pattern we only return things that are necessary for external access for our module to work perfectly as intended. This create a public gateway to our module and get access to only this that we want to reveal.

code7

6. Extending our module

There might be some cases that our built method doesn’t work fully and lack some of the functionality needed on some cases specifically. In that case we can easily extend our pre built module and add some other functionality within it.

code8

Here we have extended our Module to NewModule that have new “newMethod” added methods.

NewModule has an argument Module || {}. Here we have passed pre-built Module to the NewModule. If this Module is not defined or “undefined” then we pass new object “{}” as argument to our NewModule and extend it.