How to better understand closures

js closures, from which direction can we better understand closures? I always feel that my understanding is not very clear

Mar.28,2022

in js, a function is a scope. If you create another function in this domain, the new function may be called a closure, that is, a function inside a function. This internal function is closed, the external can not access its internal variables, and the internal can access the external variables, like a closed space, you can go out, but no one else can come in. For more details, please click https://codeshelper.com/a/11.


the main purpose is to hide variables / functions and realize private variables / functions ah


js elevation says: from a technical point of view, all JavaScript functions are closures, here we should discuss closures from a practical point of view.
you can take a look at this article closure


I think it's more intuitive to start with the scope of the variable. You can take a look at this article, portal


title

Secrets of the JavaScript Ninja,Second Edition

author

JOHN RESIG
BEAR BIBEAULT
and JOSIP MARAS

Chapter IV

Functions for the journeyman: understanding function invocation

Chapter 5

Functions for the master: closures and scopes

after reading these two chapters, the 2016 book should be able to create a clear image.
the whole book is also wonderful. JOHN RESIG, one of the authors, has written about jQuery before.


closure either wraps the box of variables so that others can't see it. Is there any other meaning?

Menu