为什么macOS软件生态不敌Windows?
优酷为什么越来越不行了?
为什么 Go 语言的 Error Handling 被某些人认为是一个败笔?
都2024年了,Golang还是不温不火吗?Go究竟能干什么?
如何评价Cursor?
微软暂停专用 Xbox 掌机开发,转而优化 Windows 11 的掌机游戏体验,这背后原因有哪些?

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

发布日期:2025-06-25 18:50:11 浏览次数:0

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

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

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

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

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