FinalizationRegistry.prototype.register()
The register() method registers an object with a
FinalizationRegistry instance so that if the object is garbage-collected,
the registry's callback may get called.
Syntax
register(target, heldValue)
register(target, heldValue, unregisterToken)
Parameters
target- : The target object to register.
heldValue- : The value to pass to the finalizer for this object. This cannot be the
targetobject but can be anything else, including functions and primitives.
- : The value to pass to the finalizer for this object. This cannot be the
unregisterTokenoptional- : A token that may be used with the
unregistermethod later to unregister the target object. If provided (and notundefined), this must be an object. If not provided, the target cannot be unregistered.
- : A token that may be used with the
Return value
undefined.
Exceptions
TypeError- : Thrown when one of the following condition is met:
targetis not an object (object as opposed to primitives; functions are objects as well)targetis the same asheldvalue(target === heldValue)unregisterTokenis not an object
- : Thrown when one of the following condition is met:
Description
See the Avoid where possible
and Notes on cleanup callbacks
sections of the FinalizationRegistry page for important caveats.