Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are closures better than global variables for preserving variables?

I understand how closures work within JavaScript, but my question is why would you go through all the trouble of making a closure to preserve a variable? Couldn't you just make the variable global? Or would that clutter up the global scope and make your code be prone to errors.

like image 848
James Fair Avatar asked Dec 27 '10 00:12

James Fair


People also ask

What are the benefits of using closures?

Closures are useful because they let you associate data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow you to associate data (the object's properties) with one or more methods.

What are two reasons why you should not use global variables?

A global variable can have no access control. It can not be limited to some parts of the program. Using global variables causes very tight coupling of code. Using global variables causes namespace pollution.

What is closure and what are the advantages of using closure?

Advantages of closuresThey allow you to attach variables to an execution context. Variables in closures can help you maintain a state that you can use later. They provide data encapsulation. They help remove redundant code. They help maintain modular code.

Why local variables are better than global?

Variables are classified into Global variables and Local variables based on their scope. The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.


2 Answers

It's a scoping issue. Global variables are just that: Global, to everyone. With closures, the scope (visibility) of the variables can be better controlled, which means the possible unintended side effects can be better controlled.

http://en.wikipedia.org/wiki/Global_variable

[Globals] are usually considered bad practice precisely because of their non-locality: a global variable can potentially be modified from anywhere (unless they reside in protected memory), and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See action at a distance

like image 127
Brian Clapper Avatar answered Sep 24 '22 04:09

Brian Clapper


Two words : race conditions.

If you set a variable in the global scope while its intended usage is local to a function instance you run the risk of having two (or more) unique instances accessing and manipulating this global variable and having all instances behave unpredictably.

There are plenty of other reasons why you should be extremely careful about storing local state in the global space.

One would be re-using the state of the last instance that set this variable (if you don't have multiple instances active simultaneously).

There are also possible conflicts with other pieces of code that rely on a global variable with the same name.

Aesthetically you also turn the global namespace into a mess (lots of random variables there without any direct information as to why they are there in the first place).

Putting variables into the global space is error-prone and makes a mess of the runtime view. The scoping possibilities of JS also make it unnecessary, which is why nobody does it (except for things that really belong there).

As an additional comment, don't mention your age or brag about your coding prowess in future questions. It's not relevant to the question.

like image 28
Michiel Kalkman Avatar answered Sep 25 '22 04:09

Michiel Kalkman