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
}