In fact, closures are not that advanced and complicated.
Just from a canonical perspective:
function a() {
var x = 1, y = 2;
return function b() {
console.log(x + y);
}
}
var f = a();
f();
During the execution of
, we can think that its local variables are stored in an internal object a, and the execution environment of Env saves a reference to a. Env
When defining
, b will also save a reference to b. Env
When
returns, the execution environment of a is destroyed, and its reference to a is gone. However, as long as Env is still accessible (e.g., b returns a via the return value). Then b's reference to b still exists, and it can still access the local variables saved in Env. Env
In the future, during the execution of function
, both b and will be accessible through the so-called scope chain x. y
After your dad gave birth to your son, you can make your own money or use your dad’s money, but you don’t want your dad to use your money.
Mapped into js. A function nests B function. The variables in B function can only be used in the scope generated by B function. A function cannot use the variables of B function. But B can use A and global scope variables.
The money in the account is a closure. This is the best explanation I can think of for ordinary people... No one knows what is inside except the account owner (caller) and the bank (memory controller) Do you have money? Although things related to the account are exposed (such as bank cards, but the passbook does not meet the requirements)
I also encountered this problem during an interview not long ago
A closure refers to a function that has permission to access variables in another scope. Generally, the memory will be released immediately after the function is executed. However, sometimes we need to "remember" the previous execution result each time the function is executed, rather than destroying it immediately. In layman's terms, closures are generally used to extend memory
Seen on the Internet: A closure is actually a nesting of functions. The inner function can use all the variables of the outer function, even if the outer function has been executed.
Local variables declared with var in the function are released after the function is executed. Global variables declared outside the function will always reside in the memory. The execution of the checkClosure function is instantaneous, and a function is created in the function body of checkClosure. The local variable str, str is not released after checkClosure is executed, because the anonymous function in setTimeout has a reference to str. After 2 seconds, the anonymous function in the function body is executed, and str is released.
Another example is PHP closing the database connection when the request ends:
$db = new mysqli('127.0.0.1','user','pass','dbname',3306);
register_shutdown_function(function() use ($db) {
$db->close();
});
The database connection $db is a variable outside the anonymous function function(). The difference between PHP and JS is that PHP needs to use use to explicitly declare the variable $db into the anonymous function.
In fact, closures are not that advanced and complicated.
Just from a canonical perspective:
During the execution of-
- When defining
- When
- In the future, during the execution of function
This is the, we can think that its local variables are stored in an internal object
a
, and the execution environment ofEnv
saves a reference toa
.Env
,
b
will also save a reference tob
.Env
returns, the execution environment of
a
is destroyed, and its reference toa
is gone. However, as long asEnv
is still accessible (e.g.,b
returnsa
via the return value). Thenb
's reference tob
still exists, and it can still access the local variables saved inEnv
.Env
, both
b
and will be accessible through the so-called scope chainx
.y
closure.
The diagram represents:returns:
a
returns:
a
This is such a high-quality answer. If I didn’t receive a notice, it must be because the interviewing unit is not efficient enough
After your dad gave birth to your son, you can make your own money or use your dad’s money, but you don’t want your dad to use your money.
Mapped into js.
A function nests B function. The variables in B function can only be used in the scope generated by B function. A function cannot use the variables of B function. But B can use A and global scope variables.
is an internal function that references external variables.
By the way, I’ll post an article I wrote before
http://scarletsky.github.io/2015/12/02/the-little-javascript-closures/
A closure is a functional.
R language is not suitable for JS.
A function that accesses its external variables is a closure
Callable object.
The money in the account is a closure. This is the best explanation I can think of for ordinary people... No one knows what is inside except the account owner (caller) and the bank (memory controller) Do you have money? Although things related to the account are exposed (such as bank cards, but the passbook does not meet the requirements)
I also encountered this problem during an interview not long ago
A closure refers to a function that has permission to access variables in another scope.
Generally, the memory will be released immediately after the function is executed. However, sometimes we need to "remember" the previous execution result each time the function is executed, rather than destroying it immediately. In layman's terms, closures are generally used to extend memory
Seen on the Internet:
A closure is actually a nesting of functions. The inner function can use all the variables of the outer function, even if the outer function has been executed.
Local variables declared with var in the function are released after the function is executed. Global variables declared outside the function will always reside in the memory. The execution of the checkClosure function is instantaneous, and a function is created in the function body of checkClosure. The local variable str, str is not released after checkClosure is executed, because the anonymous function in setTimeout has a reference to str. After 2 seconds, the anonymous function in the function body is executed, and str is released.
Another example is PHP closing the database connection when the request ends:
The database connection $db is a variable outside the anonymous function function(). The difference between PHP and JS is that PHP needs to use use to explicitly declare the variable $db into the anonymous function.