JavaScript: Prevent Adding Property

By Xah Lee. Date: . Last updated: .

What's Extensible?

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

ECMAScript® 2016 Language Specification#table-5

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 JavaScript: Property Attributes

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?

console.log ( Object.isExtensible ( Object ) ); // true
console.log ( Object.isExtensible ( Array ) ); // true
console.log ( Object.isExtensible ( Function ) ); // true
console.log ( Object.isExtensible ( String ) ); // true
console.log ( Object.isExtensible ( Date ) ); // true
console.log ( Object.isExtensible ( RegExp ) ); // true
// ...

〔►see JavaScript: What's Object?

Check If Object is Extensible

Object.isExtensible(obj)

〔►see JavaScript: Object.isExtensible

Prevent Adding Properties

Object.preventExtensions(obj)

〔►see JavaScript: Object.preventExtensions

Prevent Adding/Deleting Properties

Object.seal(obj)

〔►see JavaScript: Object.seal

Object.isSealed(obj)

〔►see JavaScript: Object.isSealed

Prevent Adding/Deleting/Writing Properties

Object.freeze(obj)

〔►see JavaScript: Object.freeze

Object.isFrozen(obj)

〔►see JavaScript: Object.isFrozen

Object and Inheritance Topic

  1. JavaScript: Object System Overview
  2. JavaScript: What's Object?
  3. JavaScript: Prototype and Inheritance
  4. JavaScript: Creating Object
  5. JavaScript: Object Literal Expression {…}
  6. JavaScript: Find Object's Prototype
  7. JavaScript: How to Create Object with Parent X?
  8. JavaScript: Prevent Adding Property
  9. JavaScript: Determine Type of Object
  10. JavaScript: Primitive Types Object Wrapper
  11. JavaScript: Clone, Deep Copy Object/Array
  12. JavaScript: Test Equality of Objects

  1. JavaScript: Object Object
  2. JavaScript: Object.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.