FrontendDeveloper.in

JavaScript question detail

How do you prevent an object from being extend

The Object.preventExtensions() method is used to prevent new properties from ever being added to an object. In other words, it prevents future extensions to the object. Let's see the usage of this property,

const newObject = {};
Object.preventExtensions(newObject); // NOT extendable

try {
Object.defineProperty(newObject, "newProperty", {
// Adding new property
value: 100,
});
} catch (e) {
console.log(e); // TypeError: Cannot define property newProperty, object is not extensible
}
Back to all JavaScript questions
Get LinkedIn Premium at Rs 399