es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
22 lines (21 loc) • 678 B
JavaScript
/**
* Deeply freezes an object and all its nested properties.
* @param {T} object - The object to freeze.
* @returns {T} The frozen object.
* @template T
* @example
* const obj = { a: 1, b: { c: 2 } };
* const frozenObj = deepFreeze(obj);
* // Attempting to modify frozenObj or its nested properties will throw an error in strict mode
*/
export function deepFreeze(object) {
Object.freeze(object);
for (const prop of Object.getOwnPropertyNames(object)) {
if (typeof object[prop] === 'object' &&
object[prop] !== null &&
!Object.isFrozen(object[prop])) {
deepFreeze(object[prop]);
}
}
return object;
}