Skip to main content
Version: 3.13.0

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.
  • unregisterToken optional
    • : A token that may be used with the unregister method later to unregister the target object. If provided (and not undefined), this must be an object. If not provided, the target cannot be unregistered.

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 as heldvalue (target === heldValue)
      • unregisterToken is not an object

Description

See the Avoid where possible and Notes on cleanup callbacks sections of the FinalizationRegistry page for important caveats.