Introduction
This article explains Closures and how to deal with them in NodeJS. Closures are functions that refer to independent variables, variables from the parent functions of the Closure remain bound from the parent's scope.
What is Closure?
A Closure is a function defined within another scope that has access to all the variables within the outer scope. A Closure allows us a free environment for the outer function to access the inner functions and inner variables without any scope restrictions.
A Closure is the local variables for a function, kept alive after the function has returned or it's a stack-frame that is not deallocated when the function returns. A Closure is an inner function that has access to the outer function variables-scope chain. The Closure has 3 scope chains; the first one is to access to its own scope (variables defined within the curly brackets) and the second one is to access the outer function's variables and the third one is to access the global variables.
Points to remember about Closures
- Whenever you use a function inside another function, a Closure is used.
- Closure in JavaScript is like keeping a copy of all the local variables.
- A Closure is created just on entry to a function and the local variables are added to that Closure.
- A new set of local variables are kept every time a function with a Closure is called.
- Closure is the term for both the functions along with the variables that are captured.
Use the following procedure to create a simple example.
Step 1:
Open Notepad and save the file with a .js extension as in the following figure:
Step 2:
Now open the NodeJS command prompt as in the following figure:
Step 3:
Write the following command in the command prompt as in the following figure:
We are manually passing the internal state around so that the other functions can get it, the greet function needs some other data, you would need to change everything to pass along more variables.
Let's have a look at another example; use the following procedure to create it.
Step 1:
Save the file with a .js extension.
Step 2:
The use of a Closure is to call a function that generates another function but hides all the state in private variables within the Closure. The greet function is nested within the greeter function. It means it's within the lexical scope of the greeter and thus depending on the rules of the Closure it has access to the local variables of the greeter including a message, name and age. "Lexical" refers to the binding of local variables and contracts with dynamic binding.
Let's have a look at another example of a Closure that creates two .js files; the first one is person.js and the other one is useclass.js.
Step 1:
Save a file with a .js extension as in the following figure:
Step 2:
Now create another file; this file will export the person module in this class, as in the following figure:
If you have a function nested inside of anything then "this" will change on you unless it's explicitly passed through or preserved with the Closure, for instance you saw the slowGreet method.
Step 3:
Now open a Node.js environment (Command Prompt) and run the preceding program as in the following figure:
Summary
A Closure is an inner function that has access to the outer function's variables (scope-chain) and Closures are extensively used in Node.js and JavaScript.