Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Reflect.getOwnPropertyDescriptor()

この記事は編集レビューを必要としています。ぜひご協力ください

これは Harmony(ECMAScript 6) 提案の一部であり、実験段階の技術です。
この技術の仕様は安定していません。ブラウザ互換性の一覧表を確認してください。またこれらの構文や動作は、仕様変更などにより、新しいバージョンのブラウザでは変更される可能性があるという点に注意してください。

静的な Reflect.getOwnPropertyDescriptor() メソッドは、Object.getOwnPropertyDescriptor() と似ています。オブジェクトにプロパティが存在する場合、与えられたプロパティのプロパティディスクリプタを返します。一方、プロパティが存在しない場合は、undefined を返します。

構文

Reflect.getOwnPropertyDescriptor(target, propertyKey)

パラメータ

target
プロパティを探す対象のオブジェクト。
propertyKey
自身のプロパティディスクリプタを取得するためのプロパティ名。

戻り値

プロパティディスクリプタオブジェクト、または undefined

スローされるエラー

target が Object ではなかった場合、TypeError がスローされる。

説明

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

Reflect.getOwnPropertyDescriptor() を使用する

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 だと、非オブジェクトである最初の引数は強制的にオブジェクトに変換されます。

Reflect.getOwnPropertyDescriptor("foo", 0);
// TypeError: "foo" は非 null オブジェクトではない

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

仕様

仕様 状態 コメント
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Reflect.getOwnPropertyDescriptor' in that specification.
Standard Initial definition.

ブラウザ実装状況

機能 Chrome Firefox (Gecko) Internet Explorer Opera Safari
基本サポート 未サポート 42 (42) 未サポート 未サポート 未サポート
機能 Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
基本サポート 未サポート 未サポート 42.0 (42) 未サポート 未サポート 未サポート

関連項目

ドキュメントのタグと貢献者

 このページの貢献者: YuichiNukiyama
 最終更新者: YuichiNukiyama,