Open In App

JavaScript Handler getPrototypeOf() Method

Last Updated : 22 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript handler.getPrototypeOf() method in JavaScript is a trap for the internal method. This method returns the same value as Object.getPrototypeOf(target) if the target object is not extensible.

Syntax: 

const p = new Proxy(obj, {
      getPrototypeOf(target) {
  ...
  }
}); 

Parameters: 

  • target: The target object.

Return value: This method always returns an object if no object is returned it returns a null.

Below examples illustrate the handler.getPrototypeOf() method in JavaScript:

Example 1: In this example, we will see the use of handler.getPrototypeOf() method in JavaScript.

javascript




const monster1 = {
    porp: 46
};
 
const monsterPrototype = {
    porp: 52
};
 
const handler = {
    getPrototypeOf(target) {
        return monsterPrototype;
    }
};
 
const proxy1 = new Proxy(monster1, handler);
console.log(Object.getPrototypeOf(proxy1) === monsterPrototype);
console.log(Object.getPrototypeOf(proxy1).porp);
 
 
let obj = {};
let p = new Proxy(obj, {
    getPrototypeOf(target) {
        return Array.prototype;
    }
});
console.log(
    p instanceof Array
);


Output: 

true
52
true

Example 2: Five ways to trigger this method of trap 

javascript




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


Output: 

true
true
true
true
true

Type of exceptions: There are two types of exceptions in handler.getPrototypeOf() method.

TypeError: “target” is not an object or null 

Example: In this example, it throws a type error in the console.

javascript




const obj = {};
const p = new Proxy(obj, {
    getPrototypeOf(target) {
        return 'Geeksforgeeks';
    }
});
console.log(Object.getPrototypeOf(p));


Output: 

Error: 'getPrototypeOf' on proxy: trap returned
neither object nor null

TypeError: expected same prototype value

Example: TypeError: expected same prototype value 

javascript




const obj = Object.preventExtensions({});
const p = new Proxy(obj, {
    getPrototypeOf(target) {
        return {};
    }
});
console.log(Object.getPrototypeOf(p));


Output: 

Error: 'getPrototypeOf' on proxy: proxy target is non-extensible
but the trap did not return its actual prototype

Supported Browsers: The browsers supported by handler.getPrototypeOf() method are listed below: 

  • Google Chrome 49 and above
  • Edge 79 and above
  • Firefox 49 and above
  • Opera 36 and above
  • Safari 10 and above

We have a complete list of Javascript Proxy/handler methods, to check those go through the Javascript Proxy/handler Reference article.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads