A
FinalizationRegistry
object lets you request a callback when an object is garbage-collected.
FinalizationRegistry
provides a way to request that a
cleanup callback
get called at some point when an object registered with the registry has been
reclaimed
(garbage-collected). (Cleanup callbacks are sometimes called
finalizers
.)
注意: Cleanup callbacks should not be used for essential program logic. See Notes on cleanup callbacks 了解细节。
You create the registry passing in the callback:
const registry = new FinalizationRegistry(heldValue => {
// ....
});
Then you register any objects you want a cleanup callback for by calling the `register` method, passing in the object and a *held value* for it:
registry.register(theObject, "some value");
The registry does not keep a strong reference to the object, as that would defeat the purpose (if the registry held it strongly, the object would never be reclaimed).
若
theObject
is reclaimed, your cleanup callback may be called at some point with the
held value
you provided for it (
"some value"
in the above). The held value can be any value you like: a primitive or an object, even
undefined
. If the held value is an object, the registry keeps a
strong
reference to it (so it can pass it to your cleanup callback later).
If you might want to unregister an object later, you pass a third value, which is the
unregistration token
you'll use later when calling the registry's
unregister
function to unregister the object. The registry only keeps a weak reference to the unregister token.
It's common to use the object itself as the unregister token, which is just fine:
registry.register(theObject, "some value", theObject); // ...some time later, if you don't care about `theObject` anymore... registry.unregister(theObject);
It doesn't have to be the same object, though; it can be a different one:
registry.register(theObject, "some value", tokenObject); // ...some time later, if you don't care about `theObject` anymore... registry.unregister(tokenObject);
FinalizationRegistry()
FinalizationRegistry
对象。
FinalizationRegistry.prototype.register()
Registers an object with the registry in order to get a cleanup callback when/if the object is garbage-collected.
FinalizationRegistry.prototype.unregister()
Unregisters an object from the registry.
Correct use of
FinalizationRegistry
takes careful thought, and it's best avoided if possible. It's also important to avoid relying on any specific behaviors not guaranteed by the specification. When, how, and whether garbage collection occurs is down to the implementation of any given JavaScript engine. Any behavior you observe in one engine may be different in another engine, in another version of the same engine, or even in a slightly different situation with the same version of the same engine. Garbage collection is a hard problem that JavaScript engine implementers are constantly refining and improving their solutions to.
Here are some specific points that the authors of the WeakRef proposal that FinalizationRegistry is part of included in its explainer document :
Garbage collectors are complicated. If an application or library depends on GC cleaning up a FinalizationRegistry or calling a finalizer [cleanup callback] in a timely, predictable manner, it's likely to be disappointed: the cleanup may happen much later than expected, or not at all. Sources of variability include:
Some notes on cleanup callbacks:
FinalizationRegistry
instance itself is no longer reachable by JavaScript code.
You create the registry passing in the callback:
const registry = new FinalizationRegistry(heldValue => {
// ....
});
Then you register any objects you want a cleanup callback for by calling the `register` method, passing in the object and a *held value* for it:
registry.register(theObject, "some value");
| 规范 |
|---|
|
WeakRefs
The definition of 'FinalizationRegistry' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
FinalizationRegistry
|
Chrome 84 | Edge 84 | Firefox 79 | IE No | Opera No | Safari No | WebView Android 84 | Chrome Android 84 | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
nodejs
13.0.0
Disabled
|
FinalizationRegistry()
构造函数
|
Chrome 84 | Edge 84 | Firefox 79 | IE No | Opera No | Safari No | WebView Android 84 | Chrome Android 84 | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
nodejs
13.0.0
Disabled
|
register
|
Chrome 84 | Edge 84 | Firefox 79 | IE No | Opera No | Safari No | WebView Android 84 | Chrome Android 84 | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
nodejs
13.0.0
Disabled
|
unregister
|
Chrome 84 | Edge 84 | Firefox 79 | IE No | Opera No | Safari No | WebView Android 84 | Chrome Android 84 | Firefox Android No | Opera Android No | Safari iOS No | Samsung Internet Android No |
nodejs
13.0.0
Disabled
|
完整支持
不支持
用户必须明确启用此特征。
FinalizationRegistry
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()