Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is the Reflect.isExtensible method in JavaScript?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Reflect.isExtensible is used to check if an object supports adding new properties to it.

Syntax

Reflect.isExtensible(target)

Parameter

target: The object to be tested for the extensibleIf the object supports the addition of new properties property. If the passed argument is not an object, then TypeError will be thrown.

Return value

Reflect.isExtensible returns true if the object is extensible and false otherwise.

Code

// New objects are extensible.
let obj = {a : 1};
console.log("The object is :", obj);
console.log("isExtensible :", Reflect.isExtensible(obj));
console.log("Trying to add a new property")
obj.b = 10;
console.log("The object is :", obj);
console.log("\nMaking object non-extensible")
Object.preventExtensions(obj);
console.log("\nisExtensible :", Reflect.isExtensible(obj));
console.log("Trying to add a new property c")
obj.c = 10;
console.log("The object is :", obj);

Explanation

In the code above:

  • We created a new object which is extensible by default.

  • We then called Reflect.isExtensible(obj). This will return true because we can add new properties to the object obj.

  • Then we added a new property with the key b to the object obj.

  • We made the object obj non-extensible by calling the Object.preventExtension method.

  • Then we called Reflect.isExtensible(obj). This will return false because we made the object obj non-extensible.

RELATED TAGS

javascript
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring