InterviewHubPRO
Section Overview
Q267

Question 267 of 521

Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

51%
JavaScript Internals (521 Qs)•Part 5: ES6+ Modern Syntax, Classes, Modules & Destructuring (254 - 328)

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,

javascript
  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
  }