JS Built-ins
← Object102 / 150
ObjectObject.preventExtensionsbeginner

Object.preventExtensions() β€” verify with isExtensible

πŸ“– 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() β€” checking with isExtensible

After calling Object.preventExtensions(), you can verify the result with Object.isExtensible().

Challenge: Implement makeAndCheckNonExtensible(obj) that locks the object and returns whether it is now non-extensible.

πŸ’‘ Hints (1)
  • β€’ preventExtensions is the weakest of the three object locking methods