JS Built-ins
← Object103 / 150
ObjectObject.preventExtensionsintermediate

Object.preventExtensions() β€” existing props modifiable

πŸ“– Usage Example
// Prevent new properties from being added
const obj = { a: 1 }
Object.preventExtensions(obj)
obj.b = 2    // silently ignored
Object.keys(obj)   // β†’ ['a']

Use Object.preventExtensions() to lock an object so no new properties can be added, while still allowing modifications to existing ones.

Object.preventExtensions() β€” mutability preserved

Object.preventExtensions() only blocks new property additions. Existing properties remain fully mutable.

Challenge: Show that existing properties are still modifiable after calling Object.preventExtensions().

πŸ’‘ Hints (1)
  • β€’ Use isExtensible to confirm the object is non-extensible