WeakRef() コンストラクター

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

WeakRef() コンストラクターは、 WeakRef オブジェクトを生成します。

構文

js
new WeakRef(target)

メモ: WeakRef()new との組み合わせでのみ構築されます。 new 無しで呼び出そうとすると TypeError が発生します。

引数

target

WeakRef の参照先となるターゲット値(リファレントとも呼ばれます)。オブジェクトまたは未登録のシンボルでなければなりません。

返値

指定されたターゲット地を参照する WeakRef オブジェクトです。

例外

TypeError

target がオブジェクトまたは未登録のシンボルではない場合に発生します。

新しい WeakRef オブジェクトの生成

完全な例は中心となる WeakRef ページを参照してください。

js
class Counter {
  constructor(element) {
    // DOM 要素への弱い参照を覚える
    this.ref = new WeakRef(element);
    this.start();
  }
}

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-weak-ref-constructor

ブラウザーの互換性

BCD tables only load in the browser

関連情報