WeakRef.prototype.deref()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2021年4月.
deref()
は WeakRef
インスタンスのメソッドで、この WeakRef
のターゲット値、またはその値が既にガベージコレクションされている場合は undefined
を返します。
構文
js
deref()
引数
なし。
返値
この WeakRef の参照先となるターゲット値で、オブジェクトまたは未登録のシンボルのどちらかです。その値が既にガベージコレクションされている場合は undefined
です。
解説
重要な注意事項については WeakRef
ページの WeakRef の注意事項の節を参照してください。
例
>deref() の使用
完全な例については WeakRef
ページ内の例の節を参照してください。
js
const tick = () => {
// 弱参照から要素を取得する (まだ存在していれば)
const element = this.ref.deref();
if (element) {
element.textContent = ++this.count;
} else {
// 要素が既に存在しない場合
console.log("この要素は消滅しました");
this.stop();
this.ref = null;
}
};
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-weak-ref.prototype.deref> |
ブラウザーの互換性
Loading…