JS: Prevent Adding Property

By Xah Lee. Date: . Last updated: .

What's Extensible?

Objects have internal slot “IsExtensible” that indicates if properties can added to the object.

Object's properties have attribute writable that determines if a property can be changed, and the attribute configurable determines if a property can be deleted,

[see JS: Property Attributes, writable, enumerable, configurable]

Object's extensible attribute and property attributes together determine if property can be added to the object, deleted, or if their values can be modified.

Note: if a object is not extensible, but its parents may be, so people can add properties to the parent object, and your object may still get unexpected properties, because of inheritance.

What Objects Are Extensible?

const arr = [ Object, Array, Function , String , Date, RegExp ];

console.log (
 arr . every (x => Object.isExtensible(x) )
); // true

[see JS: Object Type]

Check If Object is Extensible

Object.isExtensible(obj)

[see JS: Object.isExtensible]

Prevent Adding Properties

Object.preventExtensions(obj)

[see JS: Object.preventExtensions]

Prevent Adding/Deleting Properties

Object.seal(obj)

[see JS: Object.seal]

Object.isSealed(obj)

[see JS: Object.isSealed]

Prevent Adding/Deleting/Writing Properties

Object.freeze(obj)

[see JS: Object.freeze]

Object.isFrozen(obj)

[see JS: Object.isFrozen]

JS Object and Inheritance

  1. Object Overview
  2. Object Type
  3. Find Object's Type
  4. Prototype Chain
  5. Create Object
  6. Object Literal Expr
  7. Create Object with Parent X
  8. Get/Set Parent
  9. Show Prototype Chain
  10. Prevent Adding Property
  11. Clone Object
  12. Test Object Equality
  13. Add Method to Prototype

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials