对于 WeakMap,"circular" 引用会被视为 "reachability" 吗?
Would a "circular" reference be treated as "reachability" for a WeakMap?
function f() {
const w = new WeakMap();
const o = {};
w.set(o, { v: o });
return w;
}
const weakMap = f();
对于给定的代码,唯一的 weakMap
项目是否被认为是可达的?那么,它会不会被垃圾回收?
PS:这个问题是从规范的角度问的,不是具体的实现。
If an object that is being used as the key of a WeakMap key/value pair is only reachable by following a chain of references that start within that WeakMap, then that key/value pair is inaccessible and is automatically removed from the WeakMap.
在您的情况下,到达 o
的唯一方法是从 weakMap
中的一个键开始,因为没有外部引用。所以,它会被认为是不可访问的。
WeakMap implementations must detect and remove such key/value pairs and any associated resources.
所以,它最终会被垃圾回收。
function f() {
const w = new WeakMap();
const o = {};
w.set(o, { v: o });
return w;
}
const weakMap = f();
对于给定的代码,唯一的 weakMap
项目是否被认为是可达的?那么,它会不会被垃圾回收?
PS:这个问题是从规范的角度问的,不是具体的实现。
If an object that is being used as the key of a WeakMap key/value pair is only reachable by following a chain of references that start within that WeakMap, then that key/value pair is inaccessible and is automatically removed from the WeakMap.
在您的情况下,到达 o
的唯一方法是从 weakMap
中的一个键开始,因为没有外部引用。所以,它会被认为是不可访问的。
WeakMap implementations must detect and remove such key/value pairs and any associated resources.
所以,它最终会被垃圾回收。