Reflect.getPrototypeOf()

静的な Reflect.getPrototypeOf() メソッドは、Object.getPrototypeOf() とほぼ同じメソッドです。これは指定したオブジェクトのプロトタイプ (つまり、内部の [[Prototype]] プロパティの値) を返します。

試してみましょう

構文

Reflect.getPrototypeOf(target)

引数

target

プロトタイプを取得する対象のオブジェクトです。

返値

与えられたオブジェクトのプロトタイプです。継承されたプロパティがない場合、null を返します。

例外

targetObject でない場合に TypeError がスローされます。

解説

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 Language Specification
# sec-reflect.getprototypeof

ブラウザーの互換性

BCD tables only load in the browser

関連情報