Reflect.getPrototypeOf()
En esta página
The Reflect.getPrototypeOf() static method is like {{jsxref("Object.getPrototypeOf()")}}. It returns the prototype of the specified object.
{{InteractiveExample("JavaScript Demo: Reflect.getPrototypeOf()")}}
```js interactive-example const object = { foo: 42, };
const proto = Reflect.getPrototypeOf(object);
console.log(proto); // Expected output: Object { }
console.log(Reflect.getPrototypeOf(proto)); // Expected output: null
## Syntax
```js-nolint
Reflect.getPrototypeOf(target)
Parameters#
target- : The target object of which to get the prototype.
Return value#
The prototype of the given object, which may be an object or null.
Exceptions#
- {{jsxref("TypeError")}}
- : Thrown if
targetis not an object.
Description#
Reflect.getPrototypeOf() provides the reflective semantic of retrieving the prototype of an object. The only difference with {{jsxref("Object.getPrototypeOf()")}} is how non-object targets are handled. Reflect.getPrototypeOf() throws a {{jsxref("TypeError")}} if the target is not an object, while Object.getPrototypeOf() coerces it to an object.
Reflect.getPrototypeOf() invokes the [[GetPrototypeOf]] object internal method of target.
Examples#
Using Reflect.getPrototypeOf()#
Reflect.getPrototypeOf({}); // Object.prototype
Reflect.getPrototypeOf(Object.prototype); // null
Reflect.getPrototypeOf(Object.create(null)); // null
Difference with Object.getPrototypeOf()#
// Same result for Objects
Object.getPrototypeOf({}); // Object.prototype
Reflect.getPrototypeOf({}); // Object.prototype
// Both throw in ES5 for non-Objects
Object.getPrototypeOf("foo"); // Throws TypeError
Reflect.getPrototypeOf("foo"); // Throws TypeError
// In ES2015 only Reflect throws, Object coerces non-Objects
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
Specifications#
{{Specifications}}
Browser compatibility#
{{Compat}}
See also#
- Polyfill of
Reflect.getPrototypeOfincore-js - es-shims polyfill of
Reflect.getPrototypeOf - {{jsxref("Reflect")}}
- {{jsxref("Object.getPrototypeOf()")}}
handler.getPrototypeOf()