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

Views :
Update time : 2025-06-26 00:30:11

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

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

先来演示一个能正常引用的: 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 Windows上有没有一分多屏和多屏合一的软件?
2025-06-22 00:55:10
Windows上有没有一分多屏和多屏合一的软件?...
Blog Post With Youtube Video 为什么这么久了还是没有主流软件开发鸿蒙版?
2025-06-22 01:25:11
为什么这么久了还是没有主流软件开发鸿蒙版?...
Blog Post With Youtube Video 你最真实(很少吐露)的择偶标准是什么?
2025-06-22 01:05:11
你最真实(很少吐露)的择偶标准是什么?...
Blog Post With Youtube Video 在职场中,分屏显示器有什么选购技巧?
2025-06-22 00:45:10
在职场中,分屏显示器有什么选购技巧?...

Leave Your Message