aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/finalizationregistry
diff options
context:
space:
mode:
Diffstat (limited to 'files/ja/web/javascript/reference/global_objects/finalizationregistry')
-rw-r--r--files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html74
-rw-r--r--files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html66
-rw-r--r--files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html152
-rw-r--r--files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html80
-rw-r--r--files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html134
5 files changed, 506 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html
new file mode 100644
index 0000000000..c25d3dd476
--- /dev/null
+++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html
@@ -0,0 +1,74 @@
+---
+title: FinalizationRegistry.prototype.cleanupSome()
+slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/cleanupSome
+tags:
+ - FinalizationRegistry
+ - JavaScript
+ - Method
+ - Prototype
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/cleanupSome
+---
+<div>{{JSRef}}</div>
+
+<p><strong>cleanupSome()</strong> メソッドは、 {{jsxref("FinalizationRegistry")}} 内のオブジェクトのうち、まだコールバックが呼ばれていないが回収されているオブジェクトの数が実装で選択された場合に、クリーンアップコールバックを起動します。このメソッドはオプションです。</p>
+
+<h2 id="Syntax" name="Syntax">構文</h2>
+
+<pre class="syntaxbox notranslate"><code><var>registry</var>.cleanupSome([<var>callback</var>]);</code>
+</pre>
+
+<h3 id="Parameters" name="Parameters">引数</h3>
+
+<dl>
+ <dt><code><var>callback</var></code> {{optional_inline}}</dt>
+ <dd>この <code>cleanupSome</code> への呼び出しによって起動されるコールバックだけに使用するコールバックを指定します。指定した場合、このコールバックは <code>FinalizationRegistry</code> で作成されたものの代わりに使用されます。</dd>
+</dl>
+
+<h3 id="Return_value" name="Return_value">返値</h3>
+
+<p><code>undefined</code> です。</p>
+
+<h2 id="Notes" name="Notes">注</h2>
+
+<p>通常、この関数を呼び出すことはありません。コールバックを適切に行うためには、 JavaScript エンジンのガベージコレクターに任せてください。この関数は主に、イベントループを発生させない、通常の JavaScript コードよりも WebAssembly で出てくる可能性の高い、長期に実行されるコードに対応するために存在します。また、コールバックが呼び出されない場合があることにも注意してください (例えば、ターゲットが回収されたレジストリ項目が存在しない場合)。</p>
+
+<p>レジストリからクリーンアップされる (クリーンアップコールバックを呼び出す) 回収オブジェクトの項目数は、実装で定義されています。実装によっては、対象となる項目を一つだけ削除したり、対象となるすべての項目を削除したり、あるいはその間のどこかで削除したりすることもあります。</p>
+
+<h2 id="Examples" name="Examples">例</h2>
+
+<h3 id="Using_cleanupSome" name="Using_cleanupSome">cleanupSome の使用</h3>
+
+<pre class="brush: js notranslate">registry.cleanupSome?.(heldValue =&gt; {
+ // ...
+});
+</pre>
+
+<p>このメソッドはオプションであり、実装によってはこのメソッドを持たない場合があります。詳細は <a href="https://github.com/whatwg/html/issues/5446">HTML issue #5446</a> を参照してください。このメソッドはオプションなので、それを呼び出す前にそのメソッドが存在することを確認する必要があります。そのための一つの方法は、上の例のように<a href="/ja/docs/Web/JavaScript/Reference/Operators/Optional_chaining">オプション連結</a> (<code>?.</code>) を使用することです。</p>
+
+<h2 id="Specifications" name="Specifications">仕様書</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">仕様書</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('WeakRefs', '#sec-finalization-registry.prototype.cleanupSome', 'FinalizationRegistry.prototype.cleanupSome')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
+
+<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
+
+<p>{{Compat("javascript.builtins.FinalizationRegistry.cleanupSome")}}</p>
+
+<h2 id="See_also" name="See_also">関連情報</h2>
+
+<ul>
+ <li>{{jsxref("FinalizationRegistry")}}</li>
+</ul>
diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html
new file mode 100644
index 0000000000..e8ca492f4d
--- /dev/null
+++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html
@@ -0,0 +1,66 @@
+---
+title: FinalizationRegistry() コンストラクター
+slug: >-
+ Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/FinalizationRegistry
+tags:
+ - Constructor
+ - FinalizationRegistry
+ - JavaScript
+ - Reference
+ - コンストラクター
+translation_of: >-
+ Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/FinalizationRegistry
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>FinalizationRegistry</code></strong> コンストラクターは、指定されたコールバックを使用する {{jsxref("FinalizationRegistry")}} オブジェクトを生成します。</p>
+
+<h2 id="Syntax" name="Syntax">構文</h2>
+
+<pre class="syntaxbox notranslate">new FinalizationRegistry([<var>callback</var>]);
+</pre>
+
+<h3 id="Parameters" name="Parameters">引数</h3>
+
+<dl>
+ <dt><code><var>callback</var></code> {{optional_inline}}</dt>
+ <dd>レジストリが使用するコールバック関数です。指定された場合、関数である必要があります。</dd>
+</dl>
+
+<h2 id="Examples" name="Examples">例</h2>
+
+<h3 id="Creating_a_new_registry" name="Creating_a_new_registry">新しいレジストリの生成</h3>
+
+<p>コールバックを渡してレジストリを生成します。</p>
+
+<pre class="brush: js notranslate">const registry = new FinalizationRegistry(heldValue =&gt; {
+ // ....
+});
+</pre>
+
+<h2 id="Specifications" name="Specifications">仕様書</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">仕様書</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('WeakRefs', '#sec-finalization-registry-constructor', 'FinalizationRegistry constructor')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
+
+<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
+
+<p>{{Compat("javascript.builtins.FinalizationRegistry.FinalizationRegistry")}}</p>
+
+<h2 id="See_also" name="See_also">関連情報</h2>
+
+<ul>
+ <li>{{jsxref("FinalizationRegistry")}}</li>
+</ul>
diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html
new file mode 100644
index 0000000000..0277d4bde9
--- /dev/null
+++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html
@@ -0,0 +1,152 @@
+---
+title: FinalizationRegistry
+slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
+tags:
+ - Class
+ - FinalizationRegistry
+ - JavaScript
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>FinalizationRegistry</code></strong> オブジェクトにより、オブジェクトがガベージコレクションで回収されるときにコールバックを要求することができます。</p>
+
+<h2 id="Description" name="Description">解説</h2>
+
+<p><code>FinalizationRegistry</code> は、レジストリに登録されているオブジェクトが<em>回収</em>される (ガベージコレクションされる) 時に<em>クリーンアップコールバック</em>を要求する方法を提供します。(クリーンアップコールバックは<em>ファイナライザー</em>と呼ばれることもあります。)</p>
+
+<div class="note">
+<p><strong>注:</strong> Cleanup callbacks should not be used for essential program logic. 詳しくは <a href="#Notes_on_cleanup_callbacks">Notes on cleanup callbacks</a> を見てください。</p>
+</div>
+
+<p>コールバックで渡すレジストリを作成します。</p>
+
+<pre class="brush: js notranslate">const registry = new FinalizationRegistry(heldValue =&gt; {
+ // ....
+});
+</pre>
+
+<p>次に、 <code>register</code> メソッドを呼び出して、クリーンアップコールバックを行いたいオブジェクトを登録し、そのオブジェクトと<em>保持値</em>を渡します。</p>
+
+<pre class="brush: js notranslate">registry.register(theObject, "some value");
+</pre>
+
+<p>レジストリがオブジェクトへの強い参照を保持すると、目的に反してしまうので、 (レジストリが強い参照を保持していれば、そのオブジェクトは決して回収されない)、強い参照は保持はしません。</p>
+
+<p><code>theObject</code> が回収された場合、クリーンアップコールバックは、指定した<em>保持値</em> (上では <code>"some value"</code>) で呼び出される可能性があります。保持値は、プリミティブでもオブジェクトでも、 <code>undefined</code> であっても構いません。保持値がオブジェクトの場合、レジストリはその値への<em>強い</em>参照を保持します (これにより、後でクリーンアップコールバックに渡すことができます)。</p>
+
+<p>オブジェクトの登録を解除したい場合は、三番目の値を渡します。 これは、後でレジストリのレジストリの <code>unregister</code> 関数をコールしてオブジェクトの登録を解除する際に使用する<em>登録解除トークン</em>です。レジストリは、登録解除トークンへの弱い参照のみを保持します。</p>
+
+<p>よくオブジェクト自身が登録解除トークンとして使われ、これは良い結果になります。</p>
+
+<pre class="brush: js notranslate">registry.register(theObject, "some value", theObject);
+// ...some time later, if you don't care about `theObject` anymore...
+registry.unregister(theObject);
+</pre>
+
+<p>ただし、同じオブジェクトである必要はありません。異なるものでも構いません。</p>
+
+<pre class="brush: js notranslate">registry.register(theObject, "some value", tokenObject);
+// ...some time later, if you don't care about `theObject` anymore...
+registry.unregister(tokenObject);
+</pre>
+
+<h2 id="Constructor" name="Constructor">コンストラクター</h2>
+
+<dl>
+ <dt>{{jsxref("FinalizationRegistry/FinalizationRegistry", "FinalizationRegistry()")}}</dt>
+ <dd>新しい <code>FinalizationRegistry</code> オブジェクト生成します。</dd>
+</dl>
+
+<h2 id="Instance_methods" name="Instance_methods">インスタンスメソッド</h2>
+
+<dl>
+ <dt>{{jsxref("FinalizationRegistry.register", "FinalizationRegistry.prototype.register()")}}</dt>
+ <dd>Registers an object with the registry in order to get a cleanup callback when/if the object is garbage-collected.</dd>
+ <dt>{{jsxref("FinalizationRegistry.unregister", "FinalizationRegistry.prototype.unregister()")}}</dt>
+ <dd>Unregisters an object from the registry.</dd>
+ <dt>{{jsxref("FinalizationRegistry.cleanupSome", "FinalizationRegistry.prototype.cleanupSome()")}}</dt>
+ <dd><em>(Optional method.)</em> Proactively requests cleanup callbacks for reclaimed objects.</dd>
+</dl>
+
+<h2 id="Avoid_where_possible" name="Avoid_where_possible">Avoid where possible</h2>
+
+<p>Correct use of <code>FinalizationRegistry</code> takes careful thought, and it's best avoided if possible. It's also important to avoid relying on any specific behaviors not guaranteed by the specification. When, how, and whether garbage collection occurs is down to the implementation of any given JavaScript engine. Any behavior you observe in one engine may be different in another engine, in another version of the same engine, or even in a slightly different situation with the same version of the same engine. Garbage collection is a hard problem that JavaScript engine implementers are constantly refining and improving their solutions to.</p>
+
+<p>Here are some specific points that the authors of the WeakRef proposal that FinalizationRegistry is part of included in its <a href="https://github.com/tc39/proposal-FinalizationRegistrys/blob/master/README.md">explainer document</a>:</p>
+
+<blockquote>
+<p><a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)">Garbage collectors</a> are complicated. If an application or library depends on GC cleaning up a FinalizationRegistry or calling a finalizer [cleanup callback] in a timely, predictable manner, it's likely to be disappointed: the cleanup may happen much later than expected, or not at all. Sources of variability include:</p>
+
+<ul>
+ <li>One object might be garbage-collected much sooner than another object, even if they become unreachable at the same time, e.g., due to generational collection.</li>
+ <li>Garbage collection work can be split up over time using incremental and concurrent techniques.</li>
+ <li>Various runtime heuristics can be used to balance memory usage, responsiveness.</li>
+ <li>The JavaScript engine may hold references to things which look like they are unreachable (e.g., in closures, or inline caches).</li>
+ <li>Different JavaScript engines may do these things differently, or the same engine may change its algorithms across versions.</li>
+ <li>Complex factors may lead to objects being held alive for unexpected amounts of time, such as use with certain APIs.</li>
+</ul>
+</blockquote>
+
+<h2 id="Notes_on_cleanup_callbacks" name="Notes_on_cleanup_callbacks">Notes on cleanup callbacks</h2>
+
+<p>Some notes on cleanup callbacks:</p>
+
+<ul>
+ <li>Developers shouldn't rely on cleanup callbacks for essential program logic. Cleanup callbacks may be useful for reducing memory usage across the course of a program, but are unlikely to be useful otherwise.</li>
+ <li>A conforming JavaScript implementation, even one that does garbage collection, is not required to call cleanup callbacks. When and whether it does so is entirely down to the implementation of the JavaScript engine. When a registered object is reclaimed, any cleanup callbacks for it may be called then, or some time later, or not at all.</li>
+ <li>It's likely that major implementations will call cleanup callbacks at some point during execution, but those calls may be substantially after the related object was reclaimed.</li>
+ <li>There are also situations where even implementations that normally call cleanup callbacks are unlikely to call them:
+ <ul>
+ <li>When the JavaScript program shuts down entirely (for instance, closing a tab in a browser).</li>
+ <li>When the <code>FinalizationRegistry</code> instance itself is no longer reachable by JavaScript code.</li>
+ </ul>
+ </li>
+</ul>
+
+<h2 id="Examples" name="Examples">例</h2>
+
+<h3 id="Creating_a_new_registry" name="Creating_a_new_registry">Creating a new registry</h3>
+
+<p>You create the registry passing in the callback:</p>
+
+<pre class="brush: js notranslate">const registry = new FinalizationRegistry(heldValue =&gt; {
+ // ....
+});
+</pre>
+
+<h3 id="Registering_objects_for_cleanup" name="Registering_objects_for_cleanup">Registering objects for cleanup</h3>
+
+<p>Then you register any objects you want a cleanup callback for by calling the `register` method, passing in the object and a *held value* for it:</p>
+
+<pre class="brush: js notranslate">registry.register(theObject, "some value");</pre>
+
+<h2 id="Specifications" name="Specifications">仕様書</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">仕様書</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('WeakRefs', '#sec-finalization-registry-objects', 'FinalizationRegistry')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
+
+<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
+
+<p>{{Compat("javascript.builtins.FinalizationRegistry")}}</p>
+
+<h2 id="See_also" name="See_also">関連情報</h2>
+
+<ul>
+ <li>{{jsxref("WeakRef")}}</li>
+ <li>{{jsxref("WeakSet")}}</li>
+ <li>{{jsxref("WeakMap")}}</li>
+</ul>
diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html
new file mode 100644
index 0000000000..fc8fbe9475
--- /dev/null
+++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html
@@ -0,0 +1,80 @@
+---
+title: FinalizationRegistry.prototype.register()
+slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/register
+tags:
+ - FinalizationRegistry
+ - JavaScript
+ - Method
+ - Prototype
+ - Reference
+ - メソッド
+translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/register
+---
+<div>{{JSRef}}</div>
+
+<p><code>register()</code> メソッドはオブジェクトを {{jsxref("FinalizationRegistry")}} インスタンスへ登録し、オブジェクトがガベージコレクションで回収された場合、レジストリのコールバックが呼び出されるようになります。</p>
+
+<h2 id="Syntax" name="Syntax">構文</h2>
+
+<pre class="syntaxbox notranslate"><code><var>registry</var>.register(<var>target</var>, <var>heldValue</var>, [<var>unregisterToken</var>]);</code>
+</pre>
+
+<h3 id="Parameters" name="Parameters">引数</h3>
+
+<dl>
+ <dt><code><var>target</var></code></dt>
+ <dd>登録する対象オブジェクトです。</dd>
+ <dt><code><var>heldValue</var></code></dt>
+ <dd>このオブジェクトのためにファイナライザーへ渡す値です。これは <code><var>target</var></code> オブジェクトにすることはできません。</dd>
+ <dt><code><var>unregisterToken</var></code> {{optional_inline}}</dt>
+ <dd>のちに対象オブジェクトを登録解除するために <code>unregister</code> メソッドと共に使用するトークンです。提供された場合 (かつ <code>undefined</code> ではない場合)、これはオブジェクトである必要があります。提供されなかった場合は、対象を登録解除することはできません。</dd>
+</dl>
+
+<h3 id="Return_value" name="Return_value">返値</h3>
+
+<p><code>undefined</code> です。</p>
+
+<h2 id="Notes" name="Notes">注</h2>
+
+<p>重要な注意点については、 {{jsxref("FinalizationRegistry")}} ページの<a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry#Avoid_where_possible">可能な限り避ける</a>と<a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry#Notes_on_cleanup_callbacks">クリーンアップコールバックに関する注意事項</a>の部分を参照してください。</p>
+
+<h2 id="Examples" name="Examples">例</h2>
+
+<h3 id="Using_register" name="Using_register">register の使用</h3>
+
+<p>次の例は <code><var>target</var></code> で参照される対象オブジェクトを保持値 <code>"some value"</code> で登録し、対象オブジェクト自身を登録解除トークンとして渡します。</p>
+
+<pre class="brush: js notranslate">registry.register(target, "some value", target);
+</pre>
+
+<p>次の例は <code><var>target</var></code> で参照される対象オブジェクトを他のオブジェクトを保持値として登録し、登録解除トークンを渡していません (すなわち <code><var>target</var></code> は登録解除できません)。</p>
+
+<pre class="brush: js notranslate">registry.register(target, {"useful": "info about target"});
+</pre>
+
+<h2 id="Specifications" name="Specifications">仕様書</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">仕様書</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('WeakRefs', '#sec-finalization-registry.prototype.register', 'FinalizationRegistry.prototype.register')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
+
+<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
+
+<p>{{Compat("javascript.builtins.FinalizationRegistry.register")}}</p>
+
+<h2 id="See_also" name="See_also">関連情報</h2>
+
+<ul>
+ <li>{{jsxref("FinalizationRegistry")}}</li>
+</ul>
diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html
new file mode 100644
index 0000000000..3b2245b146
--- /dev/null
+++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html
@@ -0,0 +1,134 @@
+---
+title: FinalizationRegistry.prototype.unregister()
+slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/unregister
+tags:
+ - FinalizationRegistry
+ - JavaScript
+ - Method
+ - Prototype
+ - Reference
+ - メソッド
+translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/unregister
+---
+<div>{{JSRef}}</div>
+
+<p><code>unregister</code> は対象のオブジェクトを {{jsxref("FinalizationRegistry")}} インスタンスから登録解除します。</p>
+
+<h2 id="Syntax" name="Syntax">構文</h2>
+
+<pre class="syntaxbox notranslate"><code><var>registry</var>.unregister(<var>unregisterToken</var>);</code>
+</pre>
+
+<h3 id="Parameters" name="Parameters">引数</h3>
+
+<dl>
+ <dt><code><var>unregisterToken</var></code></dt>
+ <dd>対象オブジェクトを登録したときに {{jsxref("FinalizationRegistry.prototype.register", "register")}} メソッドで使用したトークンです。</dd>
+</dl>
+
+<h3 id="Return_value" name="Return_value">返値</h3>
+
+<p><code>undefined</code> です。</p>
+
+<h2 id="Notes" name="Notes">注</h2>
+
+<p>対象オブジェクトの回収が完了すると、レジストリに登録された状態ではなくなります。クリーンアップコールバックですべてに <code>unregister</code> を行う必要はありません。クリーンアップコールバックを受信しておらず、クリーンアップコールバックを受信する必要がなくなった場合にのみ <code>unregister</code> を呼び出してください。</p>
+
+<h2 id="Examples" name="Examples">例</h2>
+
+<h3 id="Using_unregister" name="Using_unregister">unregister の使用</h3>
+
+<p>この例では、登録解除トークンとして同じオブジェクトを使用して対象のオブジェクトを登録し、その後、 <code>unregister</code> を介して対象のオブジェクトの登録を解除します。</p>
+
+<pre class="brush: js notranslate">class Thingy {
+ #cleanup = label =&gt; {
+ // ^^^^^−−−−− held value
+ console.error(
+ `The \`release\` method was never called for the object with the label "${label}"`
+ );
+ };
+ #registry = new FinalizationRegistry(this.#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
+ }
+}
+</pre>
+
+<p>この例では、登録解除トークンとして別のオブジェクトを使用して対象のオブジェクトを登録しています。</p>
+
+<pre class="brush: js notranslate"> {
+ // ^^^^−−−−− held value
+ console.error(
+ `The \`release\` method was never called for the \`Thingy\` for the file "${file.name}"`
+ );
+ };
+ #registry = new FinalizationRegistry(this.#cleanup);
+
+ /**
+ * 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;
+ }
+ }
+}
+</pre>
+
+<h2 id="Specifications" name="Specifications">仕様書</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">仕様書</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('WeakRefs', '#sec-finalization-registry.prototype.unregister', 'FinalizationRegistry.prototype.unregister')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
+
+<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
+
+<p>{{Compat("javascript.builtins.FinalizationRegistry.unregister")}}</p>
+
+<h2 id="See_also" name="See_also">関連情報</h2>
+
+<ul>
+ <li>{{jsxref("FinalizationRegistry")}}</li>
+</ul>