views:

202

answers:

5

Am wrapping my head around JavaScript closures and am at a point where things are falling in place; i.e a closure is the local variables for a function - kept alive after the function has returned, or a closure is a stack-frame which is not deallocated when the function returns.

Am starting to understand this concept, but the more i understand the more i keep on wondering why do we have to use them.

An example like this one makes me understand the concept but leaves me asking, there is a simpler way of doing this!

function sayHello(name) {
   var text = 'Hello ' + name;
   var sayAlert = function() { alert(text); }
   sayAlert();
}
sayHello('Gath');

Am just wondering why do i have to keep local variable alive? after the function has exited?

Where can i get examples showing of solutions implemented by closure and that nothing else would have worked but closures?

A: 

The example you chose appears on this page, so I assume you took it from there. Have you looked at all the other examples it provides? They explain the motivations for closures better than I possibly could.

Marcelo Cantos
Yes i did, but still they don't showcase a scenario where everything else failed and closure was the only thing that could do the trick
gath
@gath - yes they do. Many of those examples are functions that have other functions as their return value. If you call the outer function, save a reference to the returned function, and then call the returned function at some later point, those examples couldn't possibly work without closures.
Joel Mueller
@gath: Everything can be written in assembler. Closures were not invented to solve problems that could never be solved before, so you won't find any problems that can only be solved with closures. They make certain problems much simpler to solve, and if you revisit those examples with this in mind, they might start to make more sense to you.
Marcelo Cantos
+2  A: 

A closure is a function with all the environnement needed for it to be executed. In javascript, it's when an anonymous function (= lambda) is created, using a variable from an outer scope.

You can better understand why with a code like that:

function foo()
{
  var text = computeFromOutside();
  // ... other lines of code
  return function(otherText) { return text + otherText; }
}

bar = foo();

function baz(fun)
{
  return fun("some text");
}

Here, you are returning a function that uses the local variable "text". Therefore, you are leaving the foo function scope, destroying its variables. However, since we have an anonymous function using text, we must keep a track of this variable. This can be achieved by value or by reference, depending on the language (Keeping the variable alive (with the possibility to modify it afterwards, or copying its value when the function is created)).

I hope this helps !

Scharron
+2  A: 

Closures add expressive power to the language. There are some patterns that can be implemented very easily because of closures. A few examples that come to mind include:

Daniel Vassallo
A: 

A normal function does its thing, computes a result and returns it. By returning a closure, you can package off your work part-way through, and let callers get more when they are ready for it. Since I tried Scheme at university, I've been hankering for closures in every language I've worked with since... they're dangerously habit-forming!

They are also arguably an important part of the future: along with other functional programming mechanism that seem a good fit for parallel programming of multi-core systems.

Pontus Gagge
A: 

check out article from EFNet #javascript FAQ

Does that link talk about function referencing or closures??
gath