FinalizationRegistry() constructor

The FinalizationRegistry() constructor creates FinalizationRegistry objects.

Syntax

js
new FinalizationRegistry(callbackFn)

Note: FinalizationRegistry() can only be constructed with new. Attempting to call it without new throws a TypeError.

Parameters

callback

A function to be invoked each time a registered target value is garbage collected. Its return value is ignored. The function is called with the following arguments:

heldValue

The value that was passed to the second parameter of the register() method when the target object was registered.

Examples

Creating a new registry

You create the registry passing in the callback:

js
const registry = new FinalizationRegistry((heldValue) => {
  // …
});

Specifications

Specification
ECMAScript Language Specification
# sec-finalization-registry-constructor

Browser compatibility

BCD tables only load in the browser

See also