Reflect.get()
静的な Reflect.get() メソッドは、オブジェクト (target[propertyKey]) からプロパティを関数として取得するように動作します。
試してみましょう
構文
js
Reflect.get(target, propertyKey[, receiver])
引数
target-
プロパティを取得する対象のオブジェクト。
propertyKey-
設定するプロパティ名。
receiver省略可-
ゲッターがあった場合、
targetへの呼び出しで使用するthisの値を提供します。Proxyとともに使用すると、targetから継承しているオブジェクトにすることができます。
返値
プロパティの値です。
例外
解説
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 Language Specification # sec-reflect.get |
ブラウザーの互換性
BCD tables only load in the browser