Reflect.getOwnPropertyDescriptor()

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.getOwnPropertyDescriptor() メソッドは Object.getOwnPropertyDescriptor() と似ています。オブジェクトにプロパティが存在する場合は、指定されたプロパティのプロパティ記述子を返します。一方、プロパティが存在しない場合は undefined を返します。

試してみましょう

const object1 = {
  property1: 42,
};

console.log(Reflect.getOwnPropertyDescriptor(object1, "property1").value);
// Expected output: 42

console.log(Reflect.getOwnPropertyDescriptor(object1, "property2"));
// Expected output: undefined

console.log(Reflect.getOwnPropertyDescriptor(object1, "property1").writable);
// Expected output: true

構文

Reflect.getOwnPropertyDescriptor(target, propertyKey)

引数

target

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

propertyKey

所有しているプロパティ記述子を取得するためのプロパティ名。

返値

target オブジェクト内にプロパティが存在する場合は、プロパティ記述子オブジェクト、または undefined

例外

TypeError: targetObject ではない場合

解説

Reflect.getOwnPropertyDescriptor オブジェクトにプロパティが存在する場合、与えられたプロパティのプロパティディスクリプタを返します。一方、プロパティが存在しない場合は、undefined を返します。Object.getOwnPropertyDescriptor() との唯一の違いは、非オブジェクトの対象がどのようにバンドルされるかだけです。

Reflect.getOwnPropertyDescriptor() の使用

js
Reflect.getOwnPropertyDescriptor({ x: "hello" }, "x");
// {value: "hello", writable: true, enumerable: true, configurable: true}

Reflect.getOwnPropertyDescriptor({ x: "hello" }, "y");
// undefined

Reflect.getOwnPropertyDescriptor([], "length");
// {value: 0, writable: true, enumerable: false, configurable: false}

Object.getOwnPropertyDescriptor() との違い

このメソッドへの最初の引数がオブジェクトではない (プリミティブであった) 場合、 TypeError が発生します。 Object.getOwnPropertyDescriptor だと、非オブジェクトである最初の引数は強制的にオブジェクトに変換されます。

js
Reflect.getOwnPropertyDescriptor("foo", 0);
// TypeError: "foo" is not non-null object

Object.getOwnPropertyDescriptor("foo", 0);
// { value: "f", writable: false, enumerable: true, configurable: false }

仕様書

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

ブラウザーの互換性

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
getOwnPropertyDescriptor

Legend

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

Full support
Full support

関連情報