JavaScript Proxy handler.getPrototypeOf() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Proxy trap

Definition

handler.getPrototypeOf(target) is a trap that runs when JavaScript asks a Proxy for its prototype, the object that it inherits from. This happens with Object.getPrototypeOf(), Reflect.getPrototypeOf() and __proto__, and also with the instanceof operator and isPrototypeOf().

The trap returns an object or null. Inside the trap you can log the question or answer with a different prototype. To get the real prototype, use Reflect.getPrototypeOf(target).

Remember
const handler = {
  getPrototypeOf(target) { ... }
};

target  → the original object

return an object → this is the prototype
return null      → no prototype
return anything else → TypeError

Syntax

Add a getPrototypeOf trap to the handler of a Proxy.

JavaScript
const fake = new Proxy({}, {
  getPrototypeOf(target) {
    return Array.prototype;
  }
});

console.log(fake instanceof Array);                          // true
console.log(Object.getPrototypeOf(fake) === Array.prototype); // true
CallWhat it does
Object.getPrototypeOf(proxy)Asks directly for the prototype. Runs the trap.
Reflect.getPrototypeOf(proxy)The function-style version.
proxy instanceof ClassWalks up the prototype chain, so it runs the trap.
Class.prototype.isPrototypeOf(proxy), proxy.__proto__Both ask for the prototype and run the trap.

A standard JavaScript Proxy trap (ES2015). You write it inside the handler object that you pass to new Proxy(). JavaScript calls it for you. No import required. Siblings: Proxy.revocable(), handler.apply(), handler.construct(), handler.defineProperty(), handler.deleteProperty(), handler.get(), handler.getOwnPropertyDescriptor(), handler.has(), handler.isExtensible(), handler.ownKeys(), handler.preventExtensions(), handler.set(), handler.setPrototypeOf().

Library declarations
JavaScript
// ECMAScript 2015 (ES6)
handler.getPrototypeOf(target)

// target  : the original object
// returns : an object or null

Parameters

ParameterDescription
targetThe original object that the Proxy wraps.

Return Value

Return ValueDescription
ObjectThe object that should count as the prototype.
nullThe Proxy should count as having no prototype.
TypeError (thrown)The trap returned something else (such as a number), or broke the rule for a target that is not extensible.

JavaScript Examples

Example 1 — Pretend to Inherit from Another Object

Make a plain Proxy answer as if its prototype were Array.prototype. The instanceof operator believes it, but Array.isArray() does not.

JavaScript
const fake = new Proxy({}, {
  getPrototypeOf(target) {
    return Array.prototype;
  }
});

console.log("instanceof Array: " + (fake instanceof Array));
console.log("instanceof Object: " + (fake instanceof Object));
console.log("Object.getPrototypeOf: " + (Object.getPrototypeOf(fake) === Array.prototype));
console.log("Reflect.getPrototypeOf: " + (Reflect.getPrototypeOf(fake) === Array.prototype));
console.log("isPrototypeOf: " + Array.prototype.isPrototypeOf(fake));
console.log("Array.isArray: " + Array.isArray(fake));
Try It Yourself

How It Works

  1. A different answer. The trap always returns Array.prototype, so Object.getPrototypeOf() and Reflect.getPrototypeOf() both report it.
  2. instanceof follows. fake instanceof Array walks up the chain, asks for the prototype, and gets Array.prototype, so it says true. The chain continues to Object.prototype, so instanceof Object is true too.
  3. Not a real array. Array.isArray() looks at what the target really is, which is a plain object, and says false.

Example 2 — See When JavaScript Asks for the Prototype

Count how often the trap runs when you test an object with instanceof, and forward the answer with Reflect.getPrototypeOf().

JavaScript
class Animal {}
class Dog extends Animal {}

let calls = 0;
const pet = new Proxy(new Dog(), {
  getPrototypeOf(target) {
    calls++;
    return Reflect.getPrototypeOf(target);
  }
});

console.log("instanceof Dog: " + (pet instanceof Dog));
console.log("instanceof Animal: " + (pet instanceof Animal));
console.log("instanceof Array: " + (pet instanceof Array));
console.log("Trap calls: " + calls);
Try It Yourself

How It Works

  1. Forward the real answer. The trap counts the call and returns Reflect.getPrototypeOf(target), which is Dog.prototype. The classes work as normal.
  2. Once per test. Each instanceof asks the Proxy for its prototype one time. After that, it follows the real chain of Dog.prototype, which is not a Proxy.
  3. Three tests, three calls. That is why the counter shows 3. This is a simple way to see where JavaScript uses the prototype.

Example 3 — Return null and Follow the Rules

A trap may answer null. It may not answer with a number, and it may not lie about a locked target. The catch blocks print the error type only.

JavaScript
const hidden = new Proxy({}, {
  getPrototypeOf() {
    return null;
  }
});
console.log("Prototype: " + Object.getPrototypeOf(hidden));
console.log("instanceof Object: " + (hidden instanceof Object));

const bad = new Proxy({}, {
  getPrototypeOf() {
    return 5;
  }
});
try {
  Object.getPrototypeOf(bad);
} catch (err) {
  console.log("Returning a number: " + err.name);
}

const locked = Object.preventExtensions({});
const liar = new Proxy(locked, {
  getPrototypeOf() {
    return Array.prototype;
  }
});
try {
  Object.getPrototypeOf(liar);
} catch (err) {
  console.log("Wrong prototype of a locked object: " + err.name);
}
Try It Yourself

How It Works

  1. null is fine. The trap says there is no prototype, so Object.getPrototypeOf() gives null and instanceof Object becomes false.
  2. Only objects or null. The number 5 is neither, so JavaScript throws a TypeError.
  3. Locked targets tell the truth. locked cannot be extended, so its prototype is fixed. The trap must return the real one (Object.prototype), so returning Array.prototype throws a TypeError.

Browser & Runtime Support

handler.getPrototypeOf() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.

✓ ES2015 · Widely supported

handler.getPrototypeOf()

Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.

ES2015 Standard since
Google Chrome 49+ · Desktop & Mobile
Full support
Microsoft Edge 12+ · Chromium and legacy
Full support
Mozilla Firefox 49+ · Desktop & Mobile
Full support
Apple Safari 10+ · macOS & iOS
Full support
Opera 36+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView 49+ · Chromium based
Supported
Node.js 6+ · Server runtime
Supported
Deno Supported · JavaScript runtime
Supported
Bun Supported · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
handler.getPrototypeOf() Excellent

Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.

Frequently Asked Questions

It is a method of the handler object that runs when JavaScript asks a Proxy for its prototype, for example with Object.getPrototypeOf(), __proto__ or the instanceof operator. It returns an object or null.
Yes. The instanceof operator walks up the prototype chain of the left side, and for a Proxy the first step uses this trap. Changing the answer changes the result of instanceof.
No. It only changes what the prototype looks like. Array.isArray() checks the target itself, and the methods of the target do not change. Use a Proxy of a real array if you need an array.
When the target is not extensible, for example after Object.preventExtensions(), Object.seal() or Object.freeze(). Then the prototype cannot change, so the trap must return the real one or a TypeError is thrown.
The trap is code that you write to intercept the question. Reflect.getPrototypeOf() is a function that returns the real prototype. Inside the trap you often return Reflect.getPrototypeOf(target) when you do not want to change anything.

Next: handler.has()

Learn how a Proxy can answer the in operator in its own way with the has trap.

handler.has() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful