Wednesday, November 12, 2025
HomeLanguagesJavascriptJavaScript Handler setPrototypeOf() Method

JavaScript Handler setPrototypeOf() Method

JavaScript handler.setPrototypeOf() method in JavaScript is a trap for Object.setPrototypeOf() method and it returns a Boolean value.

Syntax: 

const p = new Proxy(target, {
      setPrototypeOf: function(target, prototype) {
  }
});

Parameters: This method accepts two parameters as mentioned above and described below: 

  • target: This parameter is the target object.
  • prototype: This parameter is the object’s new prototype or null.

Return value: This method returns a boolean value. It returns true if the [[Prototype]] was successfully changed.

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

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

javascript




const handler1 = {
    setPrototypeOf(gfg, gfgProto) {
        gfg.geneticallyModified = true;
        return false;
    }
};
 
const gfgProto = {};
const gfg = {
    geneticallyModified: false
};
 
const proxy1 = new Proxy(gfg, handler1);
console.log(Reflect.setPrototypeOf(proxy1, gfgProto));
console.log(gfg.geneticallyModified);
 
let soo = {
    foo: 1
}
let proxy = new Proxy(soo, {
    setPrototypeOf(target, newProto) {
    }
});
console.log('a' in proxy);


Output: 

false
true
false

Example 2: In this example, we will see the use of the handler.setPrototypeOf() method in JavaScript. It throws a custom error.

javascript




const handlerThrows = {
    setPrototypeOf(target, newProto) {
        throw new Error('custom error');
    }
};
 
const newProto = {}, target = {};
 
const p2 = new Proxy(target, handlerThrows);
console.log(Object.setPrototypeOf(p2, newProto));
console.log(Reflect.setPrototypeOf(p2, newProto));


Output: 

Error: custom error

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

  • Google Chrome 49 and above
  • Edge 12 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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6763 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11983 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6834 POSTS0 COMMENTS
Umr Jansen
6838 POSTS0 COMMENTS