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
target
object but can be anything else, including functions and primitives.
- : The value to pass to the finalizer for this object. This cannot be the
unregisterToken
optional- : A token that may be used with the
unregister
method 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:
target
is not an object (object as opposed to primitives; functions are objects as well)target
is the same asheldvalue
(target === heldValue
)unregisterToken
is 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.