Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused by closures in JavaScript [duplicate]

Possible Duplicate:
How do JavaScript closures work?
Javascript closures and side effects in plain English? (separately)

I'm new to JavaScript but I'm really confused by how closures work. Can someone explain in layman's terms what they are or why they are useful?

like image 972
user1019031 Avatar asked Nov 17 '11 17:11

user1019031


People also ask

What are the disadvantages of closures in JavaScript?

There are two main disadvantages of overusing closures: The variables declared inside a closure are not garbage collected. Too many closures can slow down your application. This is actually caused by duplication of code in the memory.

What is difference between closure and lexical scope?

The lexical scope allows a function scope to access statically the variables from the outer scopes. Finally, a closure is a function that captures variables from its lexical scope. In simple words, the closure remembers the variables from the place where it is defined, no matter where it is executed.

Is closure unique to JavaScript?

Closures are a very powerful yet underused feature unique to of JavaScript (and other ECMAScript languages). They essentially provide your code with private variables that other scripts can't access.

What are the advantages of closures in JavaScript?

The advantage of closures in javascript is that it allows you to bind a variable to an execution context. var closedIn = {}; var f = function(){ closedIn. blah = 'blah'; // closedIn was just "closed in" because I used in the function, but it was defined outside the function. }


2 Answers

Closures are something like the context of a function when it is defined. Whenever a function is defined, the context is stored, and even if the 'normal' life cycle of your function is over, if you keep a reference to an element defined whithin your function execution, it can still access to elements of the context (closure), which is actually the scope of your function in its definition. Sorry for my bad english, but probably this example will make you understand:

function test() {   var a = "hello world";   var checkValue = function() { alert(a); };   checkValue();   a = "hi again";   return checkValue; }  var pointerToCheckValue = test();  //it will print "hello world" and a closure will be created with the context where the checkValue function was defined. pointerToCheckValue(); //it will execute the function checkValue with the context (closure) used when it was defined, so it still has access to the "a" variable 

Hope it helps :-)

like image 75
Alvaro Prieto Avatar answered Sep 21 '22 22:09

Alvaro Prieto


If you start with a simple use, which I got from http://ejohn.org/apps/learn/#49

var num = 10;   function addNum(myNum){    return num + myNum;  }   assert( addNum(5) == 15, "Add two numbers together, one from a closure." ); 

What is happening is that the variable num is trapped (enclosed) within the addNum function.

Where this becomes handy is if you have something (this is not expected to run properly) like this:

for(var t = 0; t < 5; t++) {   var elem = document.getElementById('mydiv' + t);   elem.onclick = function(e) {      alert(t);   }; }; 

This should show the value 5 for every div that was set with this event handler.

If you enclose that instance of the counter within your event handler then it can be different for each one, which is the expected behavior.

This is a pretty advanced topic. Once you get more comfortable with javascript you may want to see about learning it at that point.

like image 35
James Black Avatar answered Sep 22 '22 22:09

James Black