Skip to main content

Overview of the JavaScript Closures


   Today I am gonna give you all an introduction to JavaScript closures, an important concept that every programmer should be familiar with. According to MDN,
 "Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created." 
It uses lexical scoping, meaning that the function will be executed with the scope in which it was defined rather than the scope in which it was invoked.
   To make the concept clear let’s take an example. Assume we need to keep track of the number of times a button is clicked on a certain web page. So the below piece of code will do it.





   If you run the above code, it’s perfectly fine and gives the number of clicks on the button as we expected. But the problem with this approach is, here the variable ‘count’ is a global variable. So any script on the page can access and change the value of it. To avoid that we can make ‘count’ a local variable by initializing it inside our function.




   The modified code will avoid the variable from getting modified unnecessarily but with the modification we’ll not be able to track the click count as the variable ‘count’ is initialized inside the function.So whenever the function is called, it resets the value of count to zero and then increments it by one. Therefore no matter how many times you click, the click count will never go beyond one.

A closure would be the perfect solution for this kind of a problem.  



   Whenever we click on the button, the outer function is called and it will return the inner function. Here it is important to know that the outer function will get executed only once. The inner function can access the variable ‘count’, and will increment it by one whenever the button is clicked.Therefore by using a closure we can avoid the variable 'count' being accessed unnecessarily while recording the number of clicks on the button. That's it!!!
   This is just an simple example of how and where we can use closures.If you are keen on learning more about JavaScript closures, you can dive in with the tutorials available on the internet.



Comments

Popular posts from this blog

Getting started with Spring Boot

In this blog post I am gonna give you all an introduction on Spring Boot, an application framework and inversion controller for the java platform. Before diving in to Spring Boot framework, let's get a brief idea about the Spring framework on top of which the Spring Boot was created. Spring is a very popular application framework for java web and enterprise and web applications which was initially written by Rod Johnson.Millions and millions of people around the world use this to make their codes high performing, easily testable and reusable.Spring framework is build on top of the Dependency Injection (DI) concept. What is Dependency Injection? When it comes to a complex java application, the classes should not be depending on one another, in order to reuse our code and to make the unit testing easy.For such situations, dependency injection is used to connect the classes together while making them independent.So what exactly happens here? Let's think of Dependency in...

Web application using OAuth 2.0 framework

This blog post describes implementing a Node js application that uses OAuth 2.0 to upload files in to tyhe google drive. In this project we'll use Google OAuth 2.0 for authentication, Google Drive API as the file uploader and Passport.js as the authentication middleware. Before we begin, let's see what OAuth 2.0 is. OAuth 2.0 is the industry-standard protocol for authorization. OAuth 2.0 supersedes the work done on the original OAuth protocol created in 2006. OAuth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices. This specification and its extensions are being developed within the  IETF OAuth Working Group .                                                                        ...

New features of Java 10

Hello all! In this blog post I am gonna tell you about the new features of java 10.I am pretty sure all of you have heard of Java 10! Well, some of you might have dive in to Java 10 as well. Noo? Then this blog post will be a good beginning for you to learn about java 10 and its new exciting features. I am not gonna tell you about all the features but I will be focusing on the features that I found more interesting. 1.Local Variable Type Inference  Java 10 onwards Java also will have the 'var' keyword like JavaScript. This allows you to specify a variable without mentioning the data type of that particular variable but Java compiler will identify the real data type of the variable you declared.And also note that this 'var' keyword can only be used when defining variables inside methods and variables inside blocks.Although Java 10 allows the users to define variables with 'var', that doesn't make Java a dynamically typed language because once the ty...