FinalizationRegistry.prototype.register
Registers a target object with the FinalizationRegistry so that the cleanup callback is called when the target is garbage collected
Syntax
registry.register(target, heldValue, unregisterToken?)Parameters
| Parameter | Type | Description |
|---|---|---|
| target | object | The target object to register |
| heldValue | any | The value passed to the cleanup callback |
| unregisterToken | object | An optional token to use for unregistering |
Return Value
undefined
Examples
const registry = new FinalizationRegistry((val: string) => console.log('Cleaned:', val))
const obj = { x: 1 }
registry.register(obj, 'object-x')const registry = new FinalizationRegistry((id: string) => {
console.log(`Releasing handle: ${id}`)
})
const token = {}
const resource = { handle: 'abc' }
registry.register(resource, 'abc', token)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
FinalizationRegistryCreates a new FinalizationRegistry that invokes a cleanup callback after a registered target object is garbage collected
FinalizationRegistry.prototype.unregisterUnregisters a target object from the FinalizationRegistry using the unregister token provided during registration
WeakRefCreates a new WeakRef object which holds a weak reference to the target object that does not prevent garbage collection
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.