Eventually you will come across the concept of a JavaScript Closure. I want to give you a step by step walkthrough on how to implement a JavaScript Closure. Along the way, you will find out yourself why it makes sense to implement certain things with JavaScript Closures. The whole source code can be found on GitHub . If you want to code along the way, make sure to set up a JavaScript project before.
Letâs say we have the following JavaScript function which just returns an object for us. The objectâs properties are based on the incoming functionâs arguments.
In our case, the function creates an object for an employee object. The function can be used to create multiple objects one by one. Itâs up to you what you are doing with these objects afterwards. For instance, put them in an array to get a list of your companyâs employees.
In order to distinguish our employees, we should give them an employee number (identifier). The identifier should be assigned internally â because from the outside when calling the function, we donât want to care about the number.
At the moment, every employee has an employee number of 1 which isnât right. It should be a unique identifier. Usually an employee number just increments by one for every joining employee in a company. However, without being able to do something from the outside, the function doesnât know how many employees it has created already. It doesnât keep track of the state.
Since a function doesnât keep any internal state , we need to move the variable outside of the function, to increment it within the function with every created employee. We keep track of the state by incrementing the number every time the function gets called.
Note: The ++ operator (called Increment Operator ) increments an integer by one. If it is used postfix (e.g. myInteger++ ), it increments the integer but returns the value from before incrementing it. If it is used prefix (e.g. ++myInteger ), it increments the integer and returns the value after incrementing it. In contrast, there exists an Decrement Operator in JavaScript too.
There is one crucial step we did to implement this feature: We moved the variable outside of the functionâs scope in order to keep track of its state. Before it was internally managed by the function and thus only the function knew about this variable. Now we moved it outside and made it available in the global scope .
Now itâs possible to mess up things with the new global scope of the variable :
Before this wasnât possible, because the employee number was hidden in the functionâs scope â inaccessible for the outside context of the function due to the scoping of the variable . Even though our feature works, the previous code snippet clearly shows that we have a potential pitfall here.
Everything we have done in our previous code snippets was changing the scope of our variable from a functionâs scope to a global scope. A JavaScript Closure will fix the problem of our variableâs scope, making it inaccessible from the outside of the function, but making it possible for the function to track its internal state. Fundamentally, the existence of scopes in programming give closures the air to breathe.
A JavaScript Closure fixes the problem of our variableâs scope. A closure makes it possible to track internal state with a variable in a function, without giving up the local scope of this variable.
The new function became a higher-order function, because the first time calling it returns a function. This returned function can be used to create our employee as we did before. However, since the surrounding function creates a stateful environment around the returned function â in this case the stateful employee number â it is called a closure.
âClosures are functions that refer to independent (free) variables. In other words, the function defined in the closure âremembersâ the environment in which it was created.â (Source: MDN web docs )
From the outside, itâs not possible to mess with the employee number anymore. Itâs not in the global scope, but in the closure of our function. Once you create your getEmployee function, which you can give any name, the employee number is kept internally as state.
Note: Itâs worth to mention that the previous implementation of a JavaScript Closure for our example is also called âfactory patternâ in software development. Basically the outer function is our factory function and the internal function our function to create an âitemâ (here employee) out of this factoryâs specification.
I hope this brief walkthrough has helped you to understand a JavaScript Closure by example. We started with our problem â the scoping of variables and the keeping track of internal state of a function â and got rid of the problem by implementing a closure for it.