What is a Closure?
A closure in programming is a function that remembers the environment in which it was created. It can access variables from its enclosing scope, even after that scope has closed.
Example 1: Basic Closure
function outerFunction() {
let outerVariable = 'I am outside!';
function innerFunction() {
console.log(outerVariable); // Accessing outerVariable
}
return innerFunction;
}
const myInnerFunction = outerFunction();
myInnerFunction(); // Outputs: 'I am outside!'
This example demonstrates a basic closure where innerFunction
accesses outerVariable
from its enclosing function outerFunction
.
Nuances and Memory Implications
Memory Retention: Variables used by the closure are retained in memory as long as the closure exists, leading to higher memory usage.
Unintended Capturing: Closures may capture variables unintentionally, leading to unexpected behaviors and memory leaks.
Example 2: Closure in a Loop
function createFunctions() {
let functions = [];
for (var i = 0; i < 5; i++) {
functions.push(function() {
console.log(i);
});
}
return functions;
}
const funcs = createFunctions();
funcs[0](); // Outputs: 5 (not 0 as expected)
funcs[1](); // Also outputs: 5
All functions in funcs
are closures referencing the same i
, seeing its value after the loop ends.
Avoiding Memory Leaks
Be mindful of what variables the closure captures.
Release closures when they are no longer needed.
Use
let
in loops for block-scoped variables.