Skip to main content
InterviewHubPRO
Section Overview
Q267

Question 267 of 536

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

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

How do you prevent an object from being extend

mid LevelCode ExamplejavascriptES6+ Modern Syntax

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
  }