Reflect.getPrototypeOf()
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.getPrototypeOf() メソッドは、Object.getPrototypeOf() とほぼ同じメソッドです。これは指定したオブジェクトのプロトタイプ (つまり、内部の [[Prototype]] プロパティの値) を返します。
試してみましょう
const object1 = {
property1: 42,
};
const proto1 = Reflect.getPrototypeOf(object1);
console.log(proto1);
// Expected output: Object { }
console.log(Reflect.getPrototypeOf(proto1));
// Expected output: null
構文
Reflect.getPrototypeOf(target)
引数
target-
プロトタイプを取得する対象のオブジェクトです。
返値
与えられたオブジェクトのプロトタイプです。継承されたプロパティがない場合、null を返します。
例外
解説
Reflect.getPrototypeOf メソッドは、指定したオブジェクトのプロトタイプ (つまり、内部の [[Prototype]] プロパティの値) を返します。
例
>Reflect.getPrototypeOf() の使用
js
Reflect.getPrototypeOf({}); // Object.prototype
Reflect.getPrototypeOf(Object.prototype); // null
Reflect.getPrototypeOf(Object.create(null)); // null
Object.getPrototypeOf() との比較
js
// Object の結果は同じです
Object.getPrototypeOf({}); // Object.prototype
Reflect.getPrototypeOf({}); // Object.prototype
// ES5 上では非Objectの結果は両方とも例外です
Object.getPrototypeOf("foo"); // Throws TypeError
Reflect.getPrototypeOf("foo"); // Throws TypeError
// ES2015 上では Reflect のみ例外で、Object は 非Object を Object として扱います
Object.getPrototypeOf("foo"); // String.prototype
Reflect.getPrototypeOf("foo"); // Throws TypeError
// To mimic the Object ES2015 behavior you need to coerce
Reflect.getPrototypeOf(Object("foo")); // String.prototype
仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-reflect.getprototypeof> |
ブラウザーの互換性
Loading…