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.

(Note, in ES5.1 (year 2011), this was called “Extensible”. It's renamed to “IsExtensible” in ES2015.)

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 JS: 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 JS: What's Object?

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

Object and Inheritance Topic

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

  1. JS: Object Object
  2. JS: Object.prototype
Like what you read? Buy JavaScript in Depth