Introduction
In JavaScript, we have different types of patterns to write our code. All web developers write many JavaScript codes every day and we do not know how clean and safe our code is. So today, in this article, I am going to tell you about basic JavaScript Patterns which will help you write an effective JavaScript code; and we are also going to see many OOPs concepts while we go through the different patterns.
Background
Let’s start a deep dive into JavaScript Patterns.
We have three types of patterns in JavaScript.
- Function as Abstraction Pattern. (Abstraction in Javascript)
- Function to Build Module Pattern. (Encapsulation in Javascript)
- Function to avoid the Global Variable Pattern. (Anonymous function in Javascript)
Let’s see one by one with live examples.
"Function as Abstraction" Pattern
If you are coming from C# language or any object-oriented programming language background, then we probably all know about abstraction.
Let me once again quickly remind you!
Abstraction
To understand the above pattern, let’s jump to our demo.
Note Here, I am not going to create any project in Visual Studio. You can use any editor to write this simple JavaScript function or else, you can directly open your browser window, open the console window in the browser by pressing F12, then press CTL+L in Chrome and CTL+Shift+L in Firefox to clear the console window and paste the below code.
Abstraction Partern Demo
- <script>
- //Function as abstraction
- var Do = function() {
- console.log("Do is Called !!!");
- }
- var DoSomething = function(f) {
- console.log("Before Do function executed !!!");
- try {
- f();
- } catch (ex) {
- console.log(ex)
- }
- console.log("After Do function executed !!!");
- }
- DoSomething(Do);
- //End of Abstraction
- </script>
Explanation
In the above program, we have declared two variables called Do and DoSomething. Using these two variables, I declare two functions.
So, what I am doing here is hiding the details of Do function inside the DoSomething function. While calling, I only call the DoSomething function which automatically gets the Do function executed.
Advantage
Advantage of this pattern is that if you want to execute some block of code before and after the specific JavaScript function executed, then it’s very helpful.
"Function to Build Module" Pattern
In this pattern, we are going to understand how we do encapsulation in JavaScript while writing function.
Note- It’s very useful pattern while doing JavaScript program. I recommend using this pattern while writing a JavaScript code.
Let’s jump into a demo.
- //Function to Build Module
- var Do = function() { //This is Global variable
- var Job1 = function() { //This is Local varibale.
- console.log("Job1 is Called !!");
- };
- var Job2 = function() {
- console.log("Job2 is called !!");
- };
- return {
- process1: Job1,
- process2: Job2
- };
- };
- var objDo = new Do();
- objDo.process1();
- objDo.process2();
- //End Build Module
Explanation
In the above program, I have declared a function using Do variable. Inside this function, I have again declared another two functions using local variables, called as Job1 and Job2. Here, I declared two functions but you can declare as many as you require.
Hence, these Job1 and Job2 are local under this Do function; so we cannot access them outside of Doing function. Here, Do has encapsulated both Job1 and Job2 function.

Madhanmohan DevarajanPosted Mar 23, 2017, 4:34 PM
Your article seems to be nice, Please keep going..