J***aScript的闭包会无法被垃圾回收吗?

Views :
Update time : 2025-06-23 19:15:15

你理解反了吧,正因为无法引用到它,所以才会被垃圾回收。

垃圾回收靠的就是从根节点开始遍历所有对象,遍历不到的就是垃圾。

先来演示一个能正常引用的: function outer() { let str = "string"; function inner() { console.log(str); } return inner; } window.foo = outer(); outer 返回的 inner 函数由于被全局变量 foo 所引用,所以相关的一系类对象都不会被垃圾回收。

在 DevTools 内存分析面板可以看到,存在一个 contex…。

J***aScript的闭包会无法被垃圾回收吗?
Related News
Read More >>
Blog Post With Youtube Video 《欧陆风云4》游戏中殖民要怎么玩?
2025-06-22 19:45:16
《欧陆风云4》游戏中殖民要怎么玩?...
Blog Post With Youtube Video Electron 和当下其他的桌面开发方法相比如何?
2025-06-22 20:05:17
Electron 和当下其他的桌面开发方法相比如何?...
Blog Post With Youtube Video 厦门房子还敢买吗?
2025-06-22 21:00:17
厦门房子还敢买吗?...
Blog Post With Youtube Video 为什么都对TLC乃至未来的QLC嗤之以鼻呢?
2025-06-22 20:25:16
为什么都对TLC乃至未来的QLC嗤之以鼻呢?...

Leave Your Message