js2

The javascript Module Pattern

Benefits

The Module Pattern keeps things very simple and easy to read and use. We can use Objects in a very cool way, and doesn’t make swollen your code with repetitive “this” and “prototype” declarations.

Starting with the Module

In order to understand what a Module can do, we need to keep present the definition of “closure” and “scope” we will need to understand what the following function does:

Closure

Is a local variable from inner-function scope, that is keeping the reference alive after the function return

Scope

Is an space inside every function in javascript, where variables and functions exists. There variables and functions are not visible outside this scope.

What the function does:

  • Creates an Anonymous function: a function with-out a namespace declaration
  • Creates Immediately-Invoked-Function-Expressions: Declares a function, which then calls itself immediately. Creating new scope, different to the global scope, thats wrap all our function logic inside them to return only what we really needs.

Accessing our new Module

After the new scope has been created, we need to namespace our code so that we can access any methods we return.

This is our new Module, we can access it directly from the global scope using the namespace. As you can see this is not safe, so let’s make this more safe and private

The Private methods

Module scope to make our methods inaccessible outside of that scope. In order to achieve this we are going to encapsulate or group ‘private and ‘public’ members.

calling Object Literal

This is where the Module comes in to save us, by allowing us to define all our private stuff locally and only return “the good parts”.

Create a namespace to wrap our Object

Add public methods

Extending methods

This idea gives us access to our Object to extend, that’s to say, have all the benefits of private scoping/functionality and then return our extension method

Create private members

To create private members, we have to use Closures. This helps us to create a private space.

What we have done is created a Closure that looks like a self executing anonymous function that returns the public methods we added earlier.

using the public methods

Module Pattern limitations

One limitation of the module pattern so far is that the entire module must be in one file. Anyone who has worked in a large code-base understands the value of splitting among multiple files. Luckily, we have a nice solution to augment modules. First, we import the module, then we add properties, then we export it

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *