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, 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]

Object and Inheritance Topic

  1. JS: Object Overview
  2. JS: Object Type
  3. JS: Determine Type of Object
  4. JS: Object Literal Expression
  5. JS: Prototype and Inheritance
  6. JS: Create Object
  7. JS: Create Object with Parent X
  8. JS: Get/Set Prototype
  9. JS: Prevent Adding Property
  10. JS: Clone, Deep Copy Object/Array
  11. JS: Test Equality of Objects
  12. JS: Object Object
  13. JS: Object.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

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

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc