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

Views :
Update time : 2025-06-24 08:45:13

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

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

先来演示一个能正常引用的: 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 黑客为什么可以做到无需知道源码的情况下找出系统漏洞?
2025-06-23 18:10:12
黑客为什么可以做到无需知道源码的情况下找出系统漏洞?...
Blog Post With Youtube Video 为什么越来越多的国内男孩,要娶国外女孩?
2025-06-23 17:20:14
为什么越来越多的国内男孩,要娶国外女孩?...
Blog Post With Youtube Video 《庆余年》中的范闲为什么突破不了大宗师?
2025-06-23 17:45:13
《庆余年》中的范闲为什么突破不了大宗师?...
Blog Post With Youtube Video Rust 使用 Result 的错误处理方式与 Golang 使用 error 的方式有什么本质区别?
2025-06-23 18:05:12
Rust 使用 Result 的错误处理方式与 Golang 使用 error 的方式有什么本质区别?...

Leave Your Message