Skip to main content

JavaScript Closures

What are closures in JavaScript?

Taken from http://jibbering.com/faq/notes/closures/:
The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions. And that those inner functions are allowed access to all of the local variables, parameters and declared inner functions within their outer function(s). A closure is formed when one of those inner functions is made accessible outside of the function in which it was contained, so that it may be executed after the outer function has returned. At which point it still has access to the local variables, parameters and inner function declarations of its outer function. Those local variables, parameter and function declarations (initially) have the values that they had when the outer function returned and may be interacted with by the inner function.
For Example:

var x;
function foo()
{
    var i = 2;
    function bar() {
        i++;
        return i;
    }
    console.log(i); //Prints 2
    bar();
    console.log(i); //Prints 3
    x = bar;
}
foo(); //Prints 2 and 3
x(); //Now i == 4
console.log(x()); //Prints 5
foo(); //Prints 2 and 3 again



Note that bar() is an inner function that has access to foo()'s local variables.  But, the most interesting part is that after foo() returns, we can still access i by calling x().

Closures inside a for loop:
Suppose you have an list in a XHTML document.  You might want to setup an event handler that does something when you click on an item.  I'll use jQuery syntax if you don't mind.

You might be tempted to write:
var listItems = $("#list li");
for(var i = 0; i < listItems.length; i++)
    listItems.eq(i).click(function() {
        alert(i);
    });

The desired behavior is that clicking first list item would display 0, clicking the second list item would display 1, and so forth.  What actually happens, is that they all display whatever listItems.length is because that is the last known value for variable i.  Closures worked in this case, but not as desired.

Fortunately, fixing this little problem is much more trivial than one might think.  Simply create another scope level!!!  Check this out...


var listItems = $("#list li");
for(var i = 0; i < listItems.length; i++)
    listItems.eq(i).click(function(i2) {
        return
function() {
            alert(i2);
        };
    }(i));

In this case, I am passing the value of i into a new inner function (gray) that returns the former function in the previous example (red).  i2 is now in a new scope level and gets a new memory location for each iteration of the for loop.  For clarity, I have changed the variable name to i2 in the inner function, but you could also call it i.  Problem solved.

This is EXTREMELY useful in Node.JS since there are almost no synchronous, blocking function calls; rather, any I/O request or other "blocking" request is made using a callback function.

Comments

Popular posts from this blog

Beware the Ides of March...in 9 days

Stupid heading for this blog, but whatever.  I was amused.   So, a lot has happened since my last entry, which I believe was sometime in January.  I have officially started a new business -- OnlineFixShop, LLC.  The web address is http://www.onlinefixshop.com/ .  Check it out!  For the next few months, my business will be focusing on home PC repair.     I am offering services that can help you:   Rid your computer of spyware and viruses Retrieve lost information and data Gain access to the Internet Increase your computer's performance and speed Learn your way around various types of software Setup a secure wireless or wired network Back-up personal and valuable data Secure your computer(s) and protect your data Eventually, I am planning to focus on repairing computers over the Internet using remote administration technology, which I have yet to design.   Right now, I am working to setup an onlin...

JavaScript Sticky Footer and Scroll Effect

This post talks about two different HTML/JavaScript effects: How to keep a page footer stuck at the bottom of the browser window. How to create a scrolling <div> without using a scroll bar OK. So... you have a website. You want a header stuck at the top of your page and the footer stuck at the bottom of your page. The stuff in the middle, you want to be able to scrollable. But, you don't want those ugly scrollbars to the right of your scrollable text. Maybe, instead, you'll have up arrows and down arrows above and below your <div>. When you mouseover the arrows, the text in the <div> will move up or down and create a scrolling effect. Suppose your page looks like this... <html> <head> <title>Test</title> </head> <body> <div style="position: relative; width: 700px; margin-left: auto; margin-right: auto;"> <div id="header">Header</div> <div id="scrollUp...

Today's Quote

This is simply a brain dump.  I'm sleepy, and I want to type out some of the thoughts currently in my head. "Luck is where preparation and opportunity meet."  This is so remarkably true, and today I'm making it a mantra.  I believe that luck is merely an illusion that we perceive, but it truly when we have prepared ourselves for the right opportunity... and then a great opportunity comes along.  Many great opportunities pass us by every day.  Once we begin to recognize them and prepare for them, then we start to experience the thrill of luck. Interestingly, as described in "Good to Great", Mr. James Collins talks about how "Level 5" leaders often attribute their great success to luck .  That's a humble way of saying, "I planned on taking advantage of every opportunity ."   Hmmm...