Articles about the concept of "closure" are overwhelming on the Internet and are basically ruined. However, sometimes I always feel that I have read so many articles and are still in a foggy way. Of course, it is because it is difficult to understand and involves a lot of knowledge. Another very important reason is that there may be certain misunderstandings in many tutorials on the Internet, or the focus is different. Let's briefly introduce what closure is through code examples.
Code Example 1:
function a(){ var webName="Wulin.com"; console.log(webName); } a()The above is a very simple piece of code. When the function is executed, it will be released from memory, and the declared local variables will also be released in memory, so they will naturally be unable to be accessed. In many examples on the Internet, most of them involve scope issues. In fact, once the scope issues are involved, they will be off topic with the concept of "closure", although scopes and "closures" have a certain relationship.
Let’s take a look at another piece of code:
function a(){ var webName="Wulin.com"; function show() { console.log(webName); } return show; } var func = a(); func();The above code forms a typical closure. After the function a() is executed, the variable webName declared inside it can still be used.
Having said so much, what exactly is a closure? Let’s summarize it below:
Closure is a concept that describes a phenomenon in which memory resides after function execution is released. As long as you grasp this core concept, closures are not difficult to understand.
The above brief analysis of the concept of javascript closure (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.