Advantages and disadvantages of closures

Advantages
1. You can read the variables inside the function
2. You can keep these local variables in memory to achieve variable data sharing.
Disadvantages
1. Because the closure will cause the variables in the function to be saved in memory, the memory consumption is very large, so the closure cannot be abused, otherwise it will cause performance problems of the web page, and may cause memory leakage in IE. The solution is to delete all unused local variables before exiting the function.
2. Closures will change the value of variables inside the parent function outside the parent function. So, if you use the parent function as an object, the closure as its public method, and the internal variable as its private value, you must be careful not to Randomly change the value of the internal variable of the parent function.

Reprinted link: https://www.jianshu.com/p/8376170fb228

Published 91 original articles · Like 82 · Visits 10,000+

Guess you like

Origin blog.csdn.net/qq_42893625/article/details/105264019