このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

Reflect.get()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年9月.

Reflect.get() は静的メソッドで、プロパティアクセサー構文と同様に実装されていますが、関数として存在します。

試してみましょう

const object = {
  x: 1,
  y: 2,
};

console.log(Reflect.get(object, "x"));
// 予想される結果: 1

const array = ["zero", "one"];

console.log(Reflect.get(array, 1));
// 予想される結果: "one"

構文

js
Reflect.get(target, propertyKey)
Reflect.get(target, propertyKey, receiver)

引数

target

プロパティを取得する対象のオブジェクト。

propertyKey

設定するプロパティ名。

receiver 省略可

ゲッターがあった場合、 target への呼び出しで使用する this の値を提供します。

返値

プロパティの値です。

例外

TypeError

target がオブジェクトではなかった場合。

解説

Reflect.get()プロパティアクセサーの反射的意味づけを提供します。つまり、Reflect.get(target, propertyKey, receiver) は意味的に次のものと同等です:

js
target[propertyKey];

通常のプロパティアクセスでは、targetreceiver は明らかに同一のオブジェクトであることに注意してください。

Reflect.get() は、target[[Get]] オブジェクト内部メソッド を呼び出します。

Reflect.get() の使用

js
// オブジェクト
const obj1 = { x: 1, y: 2 };
Reflect.get(obj1, "x"); // 1

// 配列
Reflect.get(["zero", "one"], 1); // "one"

// 取得ハンドラー付きプロキシー
const obj2 = new Proxy(
  { p: 1 },
  {
    get(t, k, r) {
      return `${k}bar`;
    },
  },
);
Reflect.get(obj2, "foo"); // "foobar"

// 取得ハンドラー及びレシーバー付きプロキシー
const obj3 = new Proxy(
  { p: 1, foo: 2 },
  {
    get(t, prop, receiver) {
      return `${receiver[prop]}bar`;
    },
  },
);
Reflect.get(obj3, "foo", { foo: 3 }); // "3bar"

仕様書

Specification
ECMAScript® 2026 Language Specification
# sec-reflect.get

ブラウザーの互換性

関連情報