( ⊙o⊙ )! ! ! This is too official. As a newbie, I can’t understand what it means! But as a curious newbie, I really want to know what "closure" is! So I finally found the legendary "Du Niang" to help! I still have a little understanding!
Personal opinion: Define another method function in the function body, and this method function is referenced by variables outside the function, then a closure is formed!
Maybe this understanding is too abstract and not so simple and easy to understand! Example:
The above code creates a closure, because when the function is executed to var C=A();, it seems that C points to function A, but in fact the function pointed to by C is function B. So when C() is executed, 2 will be output; in this case, function B is referenced by variables other than function A. Based on personal opinion, a closure is created!
When I learned about closures in a small way, I also looked at the uses of closures. It is said that there are two main uses of closures:
1. You can read the variables inside the function
2. Keep these variables in memory at all times
The expression is completed, please give us your advice!