23
loading...
This website collects cookies to deliver better user experience
closure
// Function daclaration - Uses the function keyword
function myFunc() = {};
// Function expression - the name can be omitted, giving an anonymous function
var a = function() {}; // name omitted
var b = function myFuncTwo() {}; //function name included
// Arrow functions - arrow function syntax is a shorter syntax for a function expression
const c = () => {};
// Global scopes are variables that are accessible from any part of the program
var e = 2 // variable declared in the global scope
const square = () => {
return e * e
}
console.log(square()) // outputs 4
// Block/local scope refers to variables declared within a block '{}'
var f = 5 // variable declared in the global scope
const times = () => {
let g = 5 // variable declared in the block/local scope
return f * g
}
console.log(times()) // Outputs 25
console.log(g) // outputs undefined, because it was defined within the times function.
Closure
- A function that is a first-class object, that has access to variables defined in the same local scope in which it was defined.// 1
function extFunc() {
// Define a variable local to extFunc
const extVar = "I used a closure"
function intFunc() {
// We can access the variable defined in the scope of extFunc within inFunc
console.log(extVar)
}
// Return the inner function. Note that we're not calling it here!
return intFunc
}
// Call the outer function, which returns the inner function
const closure = extFunc()
// Call the returned function that we stored as a variable
closure()
// outputs 'I used a closure'
// 2
const seconds = 60
const text = "minutes is equal to"
function timeConversion() {
let minutes = 2
return function minutesToSeconds() {
const minToSec = `${minutes} ${text} ${seconds * minutes} seconds`
return minToSec
}
}
const convert = timeConversion()
console.log(convert()) // outputs "2 minutes is equal to 120 seconds"
console.log(timeConversion()()) // outputs "2 minutes is equal to 120 seconds"
// 3
function scores() {
var score = 85
function displayScore() {
alert(score);
}
displayScore();
}
const showScore = scores();
showScore();
extFunc()
creates a local variable called extVar
and a function called intFunc()
. The intFunc()
function is an inner function that is defined inside extFunc()
and is available only within the body of the extFunc()
function. Note that the intFunc()
function has no local variables of its own. However, since inner functions have access to the variables of outer functions, intFunc()
can access the variable name declared in the parent function, extFunc()
.return intFunc
line in 1 can be avoided by returning the internal function at the time of declaration.// switching the code in 3 from alert to console.log
function scores() {
var score = 85;
function displayScore() {
console.log(score);
}
displayScore();
}
const showScore = scores();
showScore(); // outputs 85 to the console
// get TypeError showScore is not a function
scores()
finishes executing, you might expect that the name variable would no longer be accessible. However, because the code still works as expected, this is obviously not the case in JavaScript.showScore
is a reference to the instance of the function displayScore
that is created when scores()
is run. The instance of displayScore
maintains a reference to its lexical environment, within which the variable name exists. For this reason, when showScore
is invoked, the variable score
remains available for use, and "85" is passed to console, followed by a TypeError.a Closure
.**CALL STACK**
, where it is executed and it data is kept in memory until it is removed.closure
to store them in place in memory where they can be accessed later. That area in memory is called a **HEAP MEMORY**
.call stack
which is short-lived, the heap memory
can store data indefinitely and decide when it's ready to be discarded.Data Encapsulation
.