JavaScript: Object.freeze

By Xah Lee. Date: . Last updated: .

Object.freeze( obj)

  1. Set the object's “extensible” attribute to false. (cannot add properties)
  2. Set ALL of the object's own property's “configurable” attributes to false. (cannot delete properties)
  3. Set ALL of the object's own property's “writable” attributes to false. (cannot change property values)

Freezing a object makes it impossible to {add, delete, change} properties.

[see Prevent Adding Property]

// freeze object

const oo = {k1:1, k2:2};

console.log(Object.getOwnPropertyDescriptor(oo, "k1"));
// { value: 1, writable: true, enumerable: true, configurable: true }

Object.freeze(oo);

console.log(Object.getOwnPropertyDescriptor(oo, "k1"));

// { value: 1,
//   writable: false,
//   enumerable: true,
//   configurable: false }

console.log(Object.getOwnPropertyDescriptor(oo, "k2"));
// { value: 2,
//   writable: false,
//   enumerable: true,
//   configurable: false }

console.log(Object.isExtensible(oo));
// false
JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Object Object

prototype