Documentación offline JavaScript main

handler.getPrototypeOf()

main Documentación oficial Licencia CC-BY-SA-2.5Descargado el 2026-08-02

En esta página

The handler.getPrototypeOf() method is a trap for the [[GetPrototypeOf]] object internal method, which is used by operations such as {{jsxref("Object.getPrototypeOf()")}}.

{{InteractiveExample("JavaScript Demo: handler.getPrototypeOf()", "taller")}}

```js interactive-example const monster = { eyeCount: 4, };

const monsterPrototype = { eyeCount: 2, };

const handler = { getPrototypeOf(target) { return monsterPrototype; }, };

const proxy = new Proxy(monster, handler);

console.log(Object.getPrototypeOf(proxy) === monsterPrototype); // Expected output: true

console.log(Object.getPrototypeOf(proxy).eyeCount); // Expected output: 2

## Syntax

```js-nolint
new Proxy(target, {
  getPrototypeOf(target) {
  }
})

Parameters#

The following parameter is passed to the getPrototypeOf() method. this is bound to the handler.

  • target
  • : The target object.

Return value#

The getPrototypeOf() method must return an object or null, representing the prototype of the target object.

Description#

Interceptions#

This trap can intercept these operations:

  • {{jsxref("Object.getPrototypeOf()")}}
  • {{jsxref("Reflect.getPrototypeOf()")}}
  • __proto__
  • {{jsxref("Object.prototype.isPrototypeOf()")}}
  • {{jsxref("instanceof")}}

Or any other operation that invokes the [[GetPrototypeOf]] internal method.

Invariants#

The proxy's [[GetPrototypeOf]] internal method throws a {{jsxref("TypeError")}} if the handler definition violates one of the following invariants:

  • The result must be either an {{jsxref("Object")}} or null.
  • If the target object is not extensible (that is, {{jsxref("Reflect.isExtensible()")}} returns false on target), the result must be the same as the result of Reflect.getPrototypeOf(target).

Examples#

Basic usage#

const obj = {};
const proto = {};
const handler = {
  getPrototypeOf(target) {
    console.log(target === obj); // true
    console.log(this === handler); // true
    return proto;
  },
};

const p = new Proxy(obj, handler);
console.log(Object.getPrototypeOf(p) === proto); // true

Five ways to trigger the getPrototypeOf trap#

const obj = {};
const p = new Proxy(obj, {
  getPrototypeOf(target) {
    return Array.prototype;
  },
});
console.log(
  Object.getPrototypeOf(p) === Array.prototype, // true
  Reflect.getPrototypeOf(p) === Array.prototype, // true
  p.__proto__ === Array.prototype, // true
  Array.prototype.isPrototypeOf(p), // true
  p instanceof Array, // true
);

Two kinds of exceptions#

```js example-bad const obj = {}; const p = new Proxy(obj, { getPrototypeOf(target) { return "foo"; }, }); Object.getPrototypeOf(p); // TypeError: "foo" is not an object or null

const obj2 = Object.preventExtensions({}); const p2 = new Proxy(obj2, { getPrototypeOf(target) { return {}; }, }); Object.getPrototypeOf(p2); // TypeError: expected same prototype value ```

Specifications#

{{Specifications}}

Browser compatibility#

{{Compat}}

See also#

  • {{jsxref("Proxy")}}
  • Proxy() constructor
  • {{jsxref("Object.getPrototypeOf()")}}
  • {{jsxref("Reflect.getPrototypeOf()")}}