Closures Flashcards
What is a closure?
closureare “the combination of a function and the lexical environment within which that function was [defined].”
How are closures formed?
Closures are created when you define a function or method. Closures use the lexical scope in effect at a function’s definition point to determine what variables that function can access. What variables are in scope during a function’s execution depend on the closure formed by the function’s definition. You can think of closure as a function combined with any variables from its lexical scope that the function needs.
Why are closures useful?
Even if those variables aren’t in the lexical scope where you invoke the function, it can still access them because Closures remember the outer function scope even after creation time.
Where are closures stored?
When you define a function, JavaScript finds all of the variable names it needs from the lexical scope that contains the function definition. It then takes those names and places them inside a special “envelope” object that it attaches to the function object. Each name in the envelope is a pointer to the original variable, not the value it contains.
let numbers = [1, 2, 3];
function printNumbers() {
console.log(numbers);
}
printNumbers(); // => [ 1, 2, 3 ]
numbers = [4, 5];
printNumbers(); // => [ 4, 5 ]
When do functions access their closures?
it first checks for local variables by a given name, then it looks to the closure if it can’t find it.
What is partial function application?
Partial function application refers to the creation of a function that can call a second function with fewer arguments than the second function expects. The created function applies the remaining arguments.
It applies some of the function’s arguments (theadd function’sfirstargument here) when called, and applies the remaining arguments when you call the returned function.
function add(first, second) {
return first + second;
}
function makeAdder(firstNumber) {
return function(secondNumber) {
return add(firstNumber, secondNumber);
};
}
let addFive = makeAdder(5);
let addTen = makeAdder(10);
console.log(addFive(3)); // 8
console.log(addFive(55)); // 60
console.log(addTen(3)); // 13
console.log(addTen(55)); // 65
How might you use partial function application to create an error handler?
function download(locationOfFile, errorHandler) {
// try to download the file
if (gotError) {
errorHandler(reasonCode);
}
}
function errorDetected(url, reason) {
// handle the error
}
download(“https://example.com/foo.txt”, /* ??? */);
function makeErrorHandlerFor(locationOfFile) {
return function(reason) {
errorDetected(locationOfFile, reason);
};
}
let url = “https://example.com/foo.txt”;
download(url, makeErrorHandlerFor(url));
how can we leverage closure to create private data?
function makeCounter() {
var count = 0; // declare a new variable
return function() {
count += 1; // references count from the outer scope
console.log(count);
};
}
var counter = makeCounter();
counter(); // 1
counter(); // 2
counter(); // 3
//counter still has access but the count variable is no longer
//in scope, making it essentially private
c