JavaScript: 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 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?

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

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

[see Object Type]

Check If Object is Extensible

Object.isExtensible( obj)

[see Object.isExtensible]

Prevent Adding Properties

Object.preventExtensions( obj)

[see Object.preventExtensions]

Prevent Adding/Deleting Properties

Object.seal( obj)

[see Object.seal]

Object.isSealed( obj)

[see Object.isSealed]

Prevent Adding/Deleting/Writing Properties

Object.freeze( obj)

[see Object.freeze]

Object.isFrozen( obj)

[see Object.isFrozen]

JavaScript Object and Inheritance

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc