Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weakmap 的原理 #62

Open
nmsn opened this issue May 22, 2023 · 1 comment
Open

weakmap 的原理 #62

nmsn opened this issue May 22, 2023 · 1 comment
Labels

Comments

@nmsn
Copy link
Contributor

nmsn commented May 22, 2023

No description provided.

@nmsn nmsn added the ES6 label May 22, 2023
@nmsn nmsn changed the title webpack weakmap 的原理 May 22, 2023
@nmsn
Copy link
Contributor Author

nmsn commented May 22, 2023

WeakMap 是在 ES6 中引入的新的数据类型,其中键是弱引用类型,值可以为任何数据类型。WeakMap 的主要特点是,如果键不再被引用,那么它所对应的键值对会自动从 WeakMap 中删除,这意味着 JavaScript 引擎可以在内存紧张的时候及时释放不再使用的内存。

WeakMap 的实现原理是,它内部维护了一个由键对象的引用弱引用而成的列表。当某个键对象的引用计数为零时,这个键对象同时也会被回收,WeakMap 内部的相应条目也会自动被删除。这个过程发生在垃圾回收器执行垃圾回收时。另外,由于 WeakMap 中的键是弱引用类型,所以无法通过键获取值的所有属性和方法,如 size、keys、values、entries、forEach 等,因此 WeakMap 没有这些属性和方法。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant