In JavaScript, only subfunctions inside a function can read local variables, so closures can be understood as "functions defined inside a function."
The concept of closure
A closure is a function that can read the internal variables of other functions.
Since in the Javascript language, only sub-functions inside the function can read local variables, closures can be simply understood as "functions defined inside a function".
So, in essence, closure is a bridge connecting the inside of the function with the outside of the function.
Uses of closures
Closures can be used in many places. It has two greatest uses. One is to read the variables inside the function as mentioned earlier, and the other is to keep the values of these variables in memory.
The above is the detailed content of What is closure in js. For more information, please follow other related articles on the PHP Chinese website!