JS Built-ins
← Object105 / 150
ObjectObject.preventExtensionsintermediate

Object.preventExtensions() β€” in strict mode throws

πŸ“– 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() β€” strict mode behavior

In strict mode, attempting to add a property to a non-extensible object throws a TypeError. In sloppy mode, it silently fails.

Challenge: Verify that adding a property to a non-extensible object silently fails in sloppy mode (does not add the property).

πŸ’‘ Hints (1)
  • β€’ Use try/catch to test strict mode behavior