Sobes.tech
Middle

How to make an object property immutable?

sobes.tech AI

Answer from AI

Making an object property immutable can be achieved in several ways:

  1. Using Object.defineProperty():

    This method allows defining or modifying properties of an object with more granular control. To create an immutable property, set the property descriptor with writable: false.

    const obj = {};
    Object.defineProperty(obj, 'myProperty', {
      value: 'Immutable value',
      writable: false, // Makes the property immutable
      enumerable: true, // Property will be visible during enumeration
      configurable: true // Property can be deleted or its descriptor changed
    });
    
    // Attempting to change the value will be ignored in non-strict mode
    // and will throw a TypeError in strict mode
    obj.myProperty = 'New value';
    console.log(obj.myProperty); // Outputs "Immutable value"
    

    If you set configurable: false, the property cannot be deleted or have its descriptor changed (including writable).

    const obj2 = {};
    Object.defineProperty(obj2, 'myProperty', {
      value: 'Immutable and non-deletable',
      writable: false,
      enumerable: true,
      configurable: false // Makes the property descriptor immutable
    });
    
    // Attempt to change the value
    obj2.myProperty = 'New value';
    console.log(obj2.myProperty); // Outputs "Immutable and non-deletable"
    
    // Attempt to delete the property
    delete obj2.myProperty;
    console.log(obj2.myProperty); // Outputs "Immutable and non-deletable"
    
  2. Using Object.freeze():

    This method "freezes" the entire object. It means you cannot add new properties, delete existing ones, modify the values of existing properties (including their enumerability and configurability), or change the object's prototype.

    const obj = {
      myProperty: 'Immutable value'
    };
    
    Object.freeze(obj);
    
    // Attempt to change the value
    obj.myProperty = 'New value';
    console.log(obj.myProperty); // Outputs "Immutable value"
    
    // Attempt to add a new property
    obj.newProperty = 'New';
    console.log(obj.newProperty); // Outputs undefined
    
    // Attempt to delete a property
    delete obj.myProperty;
    console.log(obj.myProperty); // Outputs "Immutable value"
    

    Note that Object.freeze() is shallow. If a property of the object is another object, that internal object will not be frozen.

  3. Using const for variables containing objects:

    The keyword const makes the reference to the object immutable, but not the object itself. You cannot reassign the variable to another object, but you can change the properties of the object.

    const obj = {
      myProperty: 'Initial value'
    };
    
    // You can change the property value
    obj.myProperty = 'New value';
    console.log(obj.myProperty); // Outputs "New value"
    
    // You cannot reassign the variable
    // obj = { anotherProperty: 'Other' }; // Will throw a TypeError
    

Comparison of methods:

Method What does it make immutable? Level of immutability
Object.defineProperty() A specific property Descriptor-based
Object.freeze() The entire object Shallow
const The reference to the object Variable-based

The choice of method depends on how granular the immutability should be. For a single property, Object.defineProperty() is suitable; for the whole object, Object.freeze(). To prevent reassignment of the reference, use const.