File size: 3,677 Bytes
780c9fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | ---
title: FinalizationRegistry.prototype.unregister()
short-title: unregister()
slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/unregister
page-type: javascript-instance-method
browser-compat: javascript.builtins.FinalizationRegistry.unregister
sidebar: jsref
---
The **`unregister()`** method of {{jsxref("FinalizationRegistry")}} instances unregisters a target value from this `FinalizationRegistry`.
## Syntax
```js-nolint
unregister(unregisterToken)
```
### Parameters
- `unregisterToken`
- : The token used with the {{jsxref("FinalizationRegistry/register", "register()")}} method when registering the target value. Multiple cells registered with the same `unregisterToken` will be unregistered together.
### Return value
A boolean value that is `true` if at least one cell was unregistered and `false` if no cell was unregistered.
### Exceptions
- {{jsxref("TypeError")}}
- : Thrown if `unregisterToken` is not an object or a [non-registered symbol](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#shared_symbols_in_the_global_symbol_registry).
## Description
When a target value has been reclaimed, it is no longer registered in the registry.
There is no need to call `unregister` in your cleanup callback. Only call
`unregister` if you haven't received a cleanup callback and no longer need
to receive one.
## Examples
### Using unregister
This example shows registering a target object using that same object as the unregister
token, then later unregistering it via `unregister`:
```js
class Thingy {
static #cleanup = (label) => {
// ^^^^^βββββ held value
console.error(
`The "release" method was never called for the object with the label "${label}"`,
);
};
#registry = new FinalizationRegistry(Thingy.#cleanup);
/**
* Constructs a `Thingy` instance.
* Be sure to call `release` when you're done with it.
*
* @param label A label for the `Thingy`.
*/
constructor(label) {
// vvvvvβββββ held value
this.#registry.register(this, label, this);
// target βββββ^^^^ ^^^^βββββ unregister token
}
/**
* Releases resources held by this `Thingy` instance.
*/
release() {
this.#registry.unregister(this);
// ^^^^βββββ unregister token
}
}
```
This example shows registering a target object using a different object as its
unregister token:
```js
class Thingy {
static #cleanup = (file) => {
// ^^^^βββββ held value
console.error(
`The "release" method was never called for the "Thingy" for the file "${file.name}"`,
);
};
#registry = new FinalizationRegistry(Thingy.#cleanup);
#file;
/**
* Constructs a `Thingy` instance for the given file.
* Be sure to call `release` when you're done with it.
*
* @param filename The name of the file.
*/
constructor(filename) {
this.#file = File.open(filename);
// vvvvvβββββ held value
this.#registry.register(this, label, this.#file);
// target βββββ^^^^ ^^^^^^^^^^βββββ unregister token
}
/**
* Releases resources held by this `Thingy` instance.
*/
release() {
if (this.#file) {
this.#registry.unregister(this.#file);
// ^^^^^^^^^^βββββ unregister token
File.close(this.#file);
this.#file = null;
}
}
}
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{jsxref("FinalizationRegistry")}}
|