0

I've recently read about closures in javascript and if I understand it correctly, it's when an inner function has access to variables in an an outer function. If so, under what use cases should I be using closures?

James Drinkard
  • 15,342
  • 16
  • 114
  • 137
  • Here's a post that's giving some good examples: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work – Ani Dec 02 '13 at 17:21
  • That's a too generic question. One golden rule of programming is to keep code ordered, well-structured and readable, while being performant: that's when you may decide to start implement enclosure. The main goal is to avoid to have an heap of static objects (variables, methods) at document level. Maybe you could use a little help reading from OOP manuals, while JS only 'emulates' objects – LittleSweetSeas Dec 02 '13 at 17:22
  • @LittleSweetSeas Do you know what "en"closure means in JavaScript? – Yuriy Galanter Dec 02 '13 at 17:26
  • 1
    @James - you don't really decide when to use it. You use it if you need it, when your code flow needs it. – Yuriy Galanter Dec 02 '13 at 17:28
  • @YuriyGalanter I do, do you know how many aspects do it share with accessibility modifiers in others high-level programming languages? Ever heard of delegates? You can write almost any JS implementation without enclosure, as well as you *can* program anything with a bunch of static method and classes. I don't mean to be rude, but I find your comment quite sarcastic. – LittleSweetSeas Dec 02 '13 at 17:31
  • @LittleSweetSeas sorry for misunderstanding, I didn't mean to be sarcasting. I just got a feeling u confused JavaScript concept of closure (when variable is preserved even when it goes out of scope) with OOP concept of encapsulating. – Yuriy Galanter Dec 02 '13 at 17:39
  • @YuriyGalanter No problem :) that's aspect of closure I find that it's similar to an instance variable - a concept related to OOP. – LittleSweetSeas Dec 02 '13 at 17:43

1 Answers1

1

When you're thinking about writing a function, but realize that that function needs to do some internal state-keeping to determine its actions. The state should be stored, read and modified in a variable — but you don't want that variable existing outside of the function (if other functions can tamper with this function's state, things could go wrong), and at the same time you can't define the variable inside the function because each time you call it that variable would be reset.

A closure is effectively simply for locking variables in an immediate scope. The easiest example is probably a toggle:

var lightSwitch = ( function closure(){
  var isLightOn = false;

  return function flickLightSwitch(){
    isLightOn = !isLightOn;

    alert( isLightOn );
  }
}() );

Imagine you're in a badly lit room, where you can't tell if the light is on or off except by flicking the switch to see the difference. When the code is executed, the closure function runs immediately, assigning the value false to isLightOn and assigning the function flickLightSwitch to the variable lightSwitch at the top. Now whenever the flickLightSwitch function is executed (called by the variable it's assigned to, lightSwitch), isLightOn becomes the opposite of whatever it used to be, and the user is alerted of its new value. External code can't read or modify the value of isLightOn because it's 'locked' in the closure function.

Barney
  • 16,181
  • 5
  • 62
  • 76