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

Views :
Update time : 2025-06-25 02:35:10

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

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

先来演示一个能正常引用的: 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-28 14:40:11
***拍大尺度片子时摄影师不会看光吗?...
Blog Post With Youtube Video 你会从mac转向Windows吗?
2025-06-28 15:00:11
你会从mac转向Windows吗?...
Blog Post With Youtube Video 个人开发者或小企业不申请经营性ICP备案,怎样开发APP盈利?
2025-06-28 15:10:11
个人开发者或小企业不申请经营性ICP备案,怎样开发APP盈利?...
Blog Post With Youtube Video ***如你财务自由,是买一个大平层还是买一个大别墅?
2025-06-28 15:10:11
***如你财务自由,是买一个大平层还是买一个大别墅?...

Leave Your Message