Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / Javascript
Tip/Trick

Javascript Closure

Rate me:
Please Sign up or sign in to vote.
4.92/5 (11 votes)
28 Oct 2014CPOL2 min read 13K   6   4
When an inner function is inside in an outer function that is called closure.

Introduction

Javascript is a fantastic programming language. It has some fantastic feature and probabely one of the importent and best feature is closure.

The technical defination of closure is something like this: "When an inner function is inside in an outer function "

Then inner function has:

  1. Access to the variable of the outer function.
  2. Access to the argument of the outer function.

Sounds confusing ??? Lets try some code:

JavaScript
//
// Any source code blocks look like this
var outerfunc=function(oname)
{
  return function(iname)
  {
    console.log(oname+" "+iname);
  };
};
var one=outerfunc("Wahid");
var two=outerfunc("Faizah");
one("Khan");
two("Malisha");
// output:
//Wahid Khan
//Faizah Malisha

So here in the above code we can clearly see there are two functions one function is inside in the another function So its a closure no doubt. The outer function name is "outerfunc" it has one argument and its return type is also a function. Actually in javascript function is a object. Sounds crazy but its true. So we can say outer function "outerfunc " have a return type of a function/object. The second function or we can say the inner function also have one argument and a console log statement.

Outside the outer function we created two functions or objects of type outerfunc with the parameter "Wahid" and "Faizah ". These two functions actually invoked the outer function with "oname" parameter. Next two line one("khan") and two("Malisha") invoked the inner function and produces the output.

In almost every other language when we reach "return" statement all the variables declare in the function will be destroyed and memory is freed up.

But in javascript when we create a closure and return from that function all the local variable will be remain in the memory and be accessible. Its a very powerful tool.

JavaScript
//
// Any source code blocks look like this
var counter=function(incnt)
{
  var initcnt=incnt;
  return function(val){
  initcnt=initcnt+val;
    console.log(initcnt);
  };
};
var x=counter(4);
x(10);
x(20);
x(30);
//14
//34
//64

In the above code  outer fuction will be called once when we instanciate it with :

JavaScript
var x=counter(4);

Variable "initcnt" initilized only once when the above line execute. So when these  lines executes:

JavaScript
x(10);
x(20);
x(30);

  It always invoke the inner function. So when the x(10) execute it got the variable initcnt value  14(4+10) when x(20) execute it got the initcnt value 34(14+20) and when x(30) execute it got the initcnt value 64(34+30). So we saw that variable initcnt initilize only once.

So this is actually the closure concept.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer IPvision Canada Inc
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Question"all the local variable will be remain in the memory and be accessible" Pin
Mehuge29-Oct-14 23:57
Mehuge29-Oct-14 23:57 
GeneralMy vote of 5 Pin
Preeti Burad29-Oct-14 22:06
Preeti Burad29-Oct-14 22:06 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)28-Oct-14 23:42
Shemeemsha (ഷെമീംഷ)28-Oct-14 23:42 
Questionnice Pin
saxenaabhi628-Oct-14 14:10
saxenaabhi628-Oct-14 14:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.