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 September 2016.

静的な Reflect.get() メソッドは、オブジェクト (target[propertyKey]) からプロパティを関数として取得するように動作します。

試してみましょう

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

console.log(Reflect.get(object1, "x"));
// Expected output: 1

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

console.log(Reflect.get(array1, 1));
// Expected output: "one"

構文

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

引数

target

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

propertyKey

設定するプロパティ名。

receiver 省略可

ゲッターがあった場合、 target への呼び出しで使用する this の値を提供します。 Proxy とともに使用すると、 target から継承しているオブジェクトにすることができます。

返値

プロパティの値です。

例外

targetObject でなかった場合、 TypeError が発生します。

解説

Reflect.get メソッドはオブジェクトのプロパティを取得します。機能としてはプロパティアクセサー構文と似ています。

Reflect.get() の使用

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

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

// Proxy with a get handler
let x = { p: 1 };

let obj = new Proxy(x, {
  get(t, k, r) {
    return k + "bar";
  },
});
Reflect.get(obj, "foo"); // "foobar"

//Proxy with get handler and receiver
let x = { p: 1, foo: 2 };
let y = { foo: 3 };

let obj = new Proxy(x, {
  get(t, prop, receiver) {
    return receiver[prop] + "bar";
  },
});
Reflect.get(obj, "foo", y); // "3bar"

仕様書

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

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
get

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報