my-blog icon indicating copy to clipboard operation
my-blog copied to clipboard

什么是闭包

Open luoxue-victor opened this issue 5 years ago • 1 comments

luoxue-victor avatar Oct 16 '19 12:10 luoxue-victor

特性:

  1. 函数嵌套函数
  2. 函数内部可以引用外部的参数和变量
  3. 参数和变量不会被垃圾回收机制回收

闭包的缺点就是常驻内存,会增大内存使用量,使用不当很容易造成内存泄露。

为什么要使用闭包:

为了设计私有方法和变量,避免全局变量污染 希望一个变量长期驻扎在内存中

view detail: https://segmentfault.com/a/1190000000652891

luoxue-victor avatar Oct 16 '19 12:10 luoxue-victor