FinalizationRegistry

FinalizationRegistry.prototype.register

Registers a target object with the FinalizationRegistry so that the cleanup callback is called when the target is garbage collected

Syntax

JavaScript
registry.register(target, heldValue, unregisterToken?)

Parameters

ParameterTypeDescription
targetobjectThe target object to register
heldValueanyThe value passed to the cleanup callback
unregisterTokenobjectAn optional token to use for unregistering

Return Value

undefined

Examples

Basic Usage
const registry = new FinalizationRegistry((val: string) => console.log('Cleaned:', val))
const obj = { x: 1 }
registry.register(obj, 'object-x')
Practical Example
const registry = new FinalizationRegistry((id: string) => {
  console.log(`Releasing handle: ${id}`)
})
const token = {}
const resource = { handle: 'abc' }
registry.register(resource, 'abc', token)
Advanced Usage
class ResourceManager {
  private registry = new FinalizationRegistry<number>(this.cleanup.bind(this))
  private resources = new Map<number, { handle: number }>()
  acquire(id: number, resource: object) {
    this.resources.set(id, { handle: id })
    this.registry.register(resource, id)
  }
  private cleanup(id: number) {
    this.resources.delete(id)
  }
}

Understanding FinalizationRegistry.prototype.register

The FinalizationRegistry.prototype.register method in JavaScript registers a target object with the FinalizationRegistry so that the cleanup callback is called when the target is garbage collected. It belongs to the FinalizationRegistry object and is one of the most widely used methods for working with finalizationregistry values in modern JavaScript and TypeScript applications.

The method signature is registry.register(target, heldValue, unregisterToken?). It accepts 3 parameters: target, heldValue, unregisterToken. When called, it returns undefined. Understanding when and how to use register() helps you write more expressive, readable code.

Common use cases for FinalizationRegistry.prototype.register include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like finalizationregistry-constructor, finalizationregistry-unregister, weakref-constructor, enabling you to chain operations together for complex data manipulation pipelines.

Supported in Chrome 84+, Firefox 79+, Safari 14.1+, Edge 84+, Node.js 14.6+.

Browser Compatibility

Supported in Chrome 84+, Firefox 79+, Safari 14.1+, Edge 84+, Node.js 14.6+.

Related Methods

More FinalizationRegistry Methods

Other methods in the FinalizationRegistry object

Related Tools

More FinalizationRegistry Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.