JavaScript: Object.freeze
Object.freeze(obj)
- Set the object's “extensible” attribute to
false
. (cannot add properties) - Set ALL of the object's own property's “configurable” attributes to
false
. (cannot delete properties) - 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