-2

Closures have always been a catch for me than a feature. But i think it has something powerful to offer. However, i understand it enough to avoid it but not enough to make use of it. Which i wish to change.

So can you give me some examples or situations when use of closure helped your code by reducing size, effort, increasing speed, portability, maintainability.

I am in same page with prototype..i know how it works, i know how to use it but i don't in general. My main style of programming is just creating tons of functions. But js offer more features so i'd like to use them.

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
  • no negative votes without explanation and time to fix it please..they are of no help and value – Muhammad Umer Mar 28 '14 at 19:01
  • Closures are **not** for speed, portability or maintainability. I am not sure why you are fixed on these. – Wiktor Zychla Mar 28 '14 at 19:11
  • See http://stackoverflow.com/questions/111102/how-do-javascript-closures-work . There's an excellent explanation there. – rupps Mar 28 '14 at 19:16
  • seems you don't if you ask this question :) – rupps Mar 28 '14 at 19:17
  • that's true to some extent...though...i mean i know them like 60%..as i said not enough to make use of them – Muhammad Umer Mar 28 '14 at 19:19
  • 1
    the other 30% is the key, you will catch up soon with a little practice. A pretty typical use is to make your javascript modules & classes bulletproof, without any dependence on global variables. This is very nice for a number of reasons: modularity, security, ease of debugging ... – rupps Mar 28 '14 at 19:23

1 Answers1

1

Closures are very natural for me and I use them all the time. You don't see the use of this?

var count = 0;

$('#id').click(function(){
 ++count; //thanks to closure I can access count
});
lyoung
  • 427
  • 2
  • 11