1. What are closures:
- A function is bound with references to its surrounding state (or surrounded by external references). This combination is called a closure - in plain English, functions that can read variables inside other functions are called closures - check with Chrome Developer Tools. A closure is a Scope object that refers to a local variable of an external function and exists in an internal functionCopy the code
2. What are internal functions:
In general, a function defined inside another function is called an inner functionCopy the code
3. Causes
- Function nesting - Internal function references external function local variables - calls external functionCopy the code
4. What closures do
(1) let the outside of the function manipulate the data inside the function (2) extend the local variable life cycleCopy the code
5. Shortcomings
- Easy to leak memory - Fix: Null the variable that holds the internal function of the closure, making the internal function a garbage object that will be collected by the garbage collection mechanismCopy the code
6. Life cycle
- Generated: calls to external functions (when the internal function definition has been executed) - Dead: When the internal function becomes a junk objectCopy the code
7. Application
- React High-order functions: To pass extra arguments to the functionCopy the code