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.
A different answer. The trap always returns Array.prototype, so Object.getPrototypeOf() and Reflect.getPrototypeOf() both report it.
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.
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);
Forward the real answer. The trap counts the call and returns Reflect.getPrototypeOf(target), which is Dog.prototype. The classes work as normal.
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.
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.
Prototype: null
instanceof Object: false
Returning a number: TypeError
Wrong prototype of a locked object: TypeError
How It Works
null is fine. The trap says there is no prototype, so Object.getPrototypeOf() gives null and instanceof Object becomes false.
Only objects or null. The number 5 is neither, so JavaScript throws a TypeError.
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.
Compatibility
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.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo 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.