JavaScript Handler isExtensible() Method



The handler.isExtensible() method in JavaScript is a part of the Proxy object. It is used to determine whether the new property can be added or not the target object. If the target object is extendable, the method returns true; if not, it returns false. This can be helpful in situations where you want to make sure that an object doesn't get altered any further or that it stays open for more additions. For example, you could use this method to check if an object is extensible before attempting to add new properties to it.

Syntax

Following is the syntax of JavaScript handler.isExtensible() method −

new Proxy(target, {
   isExtensible(target) {}
});

Parameters

  • target − It holds the target object.

Return value

This method returns a Boolean value.

Example 1

Let's look at the following example, where we are going to check the extensibility of an object.

<html>
<style>
body {
   font-family: verdana;
   color: #DE3163;
}
</style>
<body>
<script>
const obj = {
   car: "POLO GT",
   model: 2023
};
document.write(Reflect.isExtensible(obj) + " < br > "); 
Object.preventExtensions(obj); document.write(Reflect.isExtensible(obj));
</script>
</body>
</html>

Output

If we execute the above program, it will displays a text on the webpage.

Example 2

Consider another scenario where we are going to expand an object by adding a new property.

<html>
<style>
body {
   font-family: verdana;
   color: #DE3163;
}
</style>
<body>
<script>
const x = {
   car: "POLO GT",
   model: 2024
};
document.write(Reflect.isExtensible(x) + " < br > "); 
x.manufacture = "INDIA"; document.write(x.manufacture + " < br > "); 
document.write(Reflect.isExtensible(x));
</script>
</body>
</html>

Output

On executing the above script, it will displays text on the webpage.

Example 3

In the following example, we are going to create a non extensible object using object.freeze().

<html>
<style>
body {
   font-family: verdana;
   color: #DE3163;
}
</style>
<body>
<script>
const x = {
   fruit: 'Apple',
   season: 'winter'
};
document.write(Reflect.isExtensible(x) + " < br > "); // true
Object.freeze(x); document.write(Reflect.isExtensible(x));
</script>
</body>
</html>

When we execute the above code, it will generate an output consisting of the text displayed on the webpage.

Advertisements