JS Built-ins
← Object104 / 150
ObjectObject.preventExtensionsintermediate

Object.preventExtensions() β€” prototype not affected

πŸ“– 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() β€” only affects own properties

Object.preventExtensions() only prevents additions to the target object itself. The prototype can still be modified.

Challenge: Understand that preventExtensions does not restrict prototype modifications.

πŸ’‘ Hints (1)
  • β€’ preventExtensions is applied only to the direct object, not its prototype chain