Function objDeepFreeze

  • Deep freezes an object, making it immutable by recursively freezing all nested objects and functions.

    Parameters

    • o: Record<string | symbol, any>

      The object to deep freeze. This function uses Object.freeze() to freeze the object and all its nested objects and functions.

    Returns Record<string | symbol, any>

    The same object, but deeply frozen.

    Throws

    If the object is not an object or function.

    See

    Object.freeze

    Example

    const obj = {
    prop1: 'value1',
    prop2: {
    nestedProp1: 'nestedValue1',
    nestedProp2: {
    deeplyNestedProp: 'deeplyNestedValue'
    }
    },
    prop3: () => {
    //=> Hello, world!
    }
    }
    const frozenObj = objDeepFreeze(obj)
    // Attempting to modify the frozen object will throw an error
    frozenObj.prop1 //=> 'value1'
    frozenObj.prop2.nestedProp1 //=> 'nestedValue1'
    frozenObj.prop2.nestedProp2.deeplyNestedProp //=> 'deeplyNestedValue'
    frozenObj.prop3() //=> undefined

Generated using TypeDoc