+1 like for your willingness to learn closure. Closures can seem really daunting, to begin with, I know they were for me. Once you get the hang of them, you’ll instantly be a much better coder.
Every Interviewer’s favorite topic is Closure. Let’s dive here on What is Closure and why do we need it. Before Jumping into Closure, first, let’s find what is Lexical Scoping.
Lexical Scoping :
Let’s try with an example:
The output of the above code is 3.Here outer() function contains inner function inner()and when outer() is called inner function is executed which can access variable a inside outer function i.e outside inner function block.
This is the output:
Let’s see a more beautiful example by Mozilla
This is not only good code to demonstrate closure, but also a good example to depict one of the major applications of closure. In the code above, we have a function that takes an argument and returns an inner function which also takes an argument.
In the code,
This creates a closure add5 and add10 when the makeAdder(5) and makeAdder(10) function is called. This returns a function and the lexical environment where x is 5 and 10 respectively. What I am trying to advocate is that both closures have separate lexical environment. So, it is not necessary that multiple closures should share the same lexical environment.
Application 1: Emulate Private scope for variable and functions:
Here, in code above speed is only accessible by accelerate otherwise, it is hidden. It is only exposed to accelerate. In general, the closure has allowed using hidden variables internally.
Another powerful example that present us the way function can be emulated private with help of closure is again taken here from Mozilla
In the example code above, three functions counter.increment, counter.value, and counter.decrement share the same lexical environment. The lexical environment is created when the self-invoking function (anonymous function) is called or executed. There are many functions and variables in anonymous functions. privateCounter variable and changeBy() function are private whereas the object method returned by the anonymous function is public. You can’t access either of these private members from outside the anonymous function. You can only access the private function and variable inside it with public functions.
Here, in the code
Another example is in the counter
Here, we can see a live demo of the above code in this pen.
Here, in the code counter variable is inaccessible from outside i.e it is a private variable that can be accessed only by updateClickCount which is the reference of the function returned. Since it is a private variable we can stay assured that it cannot be tempered from other parts of code as it isn’t exposed globally and vulnerably.
Application number 2: Closures are very important in functional programming as they are used for two concepts — partial application and currying:
Providing less argument to a function than the function has expected is a partial application of a function.
We call a function with fewer arguments than it expects and it returns a function that takes the remaining arguments. This is called a partial application of functions.
There is always a topic of discussion on Partial function and currying. I wanna diminish this with one line definition of both:
# Partial Application :
A function is partially applied when it is given a fewer argument than it expects and returns a new function expecting the remaining arguments. Partial application fixes one or more arguments inside the returned function
# Currying :
A function is curried when it takes one argument at a time and returns a new function expecting the next argument.
let’s write a small code first and we will approach how closure is involved in this partial application of a function:
The block of code below is an example code to demonstrate currying to the very same function addFourNumber() from the above code. Let’s look through the code and try to understand how currying is basically closure.
Before jumping to code, let’s remember first that the function is gonna accept each argument one at a time and returning a function each time until it has all the arguments and then return the value of the function body.
If you are confused and can’t understand bind() and had a hard time understanding a chunk of code above currying’s section, have a look at the makeAdder() code we went through earlier. Our previous code of adder is :
This is also a partially applied function example code which is the same as binding in the above code. Here, the code itself has makeAdder() function which returns closure of inner function.
The main benefit to closures is you can “partially apply” a function using a closure, then pass the partially applied function around, instead of needing to pass the non-applied function, and any data you’ll need to call it (very useful, in many scenarios).
In Other Words:
Closures are really useful when we are dealing with higher-order functions especially when we want to communicate state.
That’s it so when the Interview presents the code with the structure like above, remember what we learned here.
It’s what I have right now in my mind. I will update this if anything new comes to my mind. Let me know if I missed something.
Thanks for reading.
Don’ forget to follow me on Twitter. I post interesting codes and articles there to keep you updated and rejuvenated.
Have a good day, Take Care and Cheers !!!!2