back to series

Implement a deepSealObject function

Object.seal() static method on the Object constructor when used on objects “seal” the object. A sealed object has a fixed set of properties: new properties cannot be added, existing properties cannot be removed, their enumerability and configurability cannot be changed, and its prototype cannot be re-assigned.

You can learn more about seal on MDN.

However, sealing only works on non-nested objects.

Variation: Deep Seal

1. Basic implementation

We have to implement a deepSealObject function which seals an object on every nested level.

We can verify whether an object is sealed or not using the Object.isSealed() static method on object constructor.

./src/deepSealObject.js
function deepSealObject(input) {
const result = {};
for (const key in input) {
// only for non inherited keys
if (Object.hasOwn(input, key)) {
const value = input[key];
if (typeof value !== 'object') {
result[key] = Object.seal(value);
} else {
// recursively keep sealing each level
result[key] = deepSealObject(value);
}
}
}
// finally seal the root
return Object.seal(result);
}
// Usage
const input = {
a: 1,
b: {
c: 2
}
};
console.log(Object.isSealed(input)); // yields false
const output = deepSealObject(input);
console.log(Object.isSealed(output)); // yields true
delete output.b.c; // no effect
console.log(output);
Variation: Deep Freeze

2. Deep freeze implementation

We also have a Object.freeze() static method available which is very similar to Object.seal(). For an interview setting, one might be asked to implement deepFreezeObject as well.

The implementation remains exactly same except, we use the Object.freeze() method instead of Object.seal().

./src/deepFreezeObject.js
function deepFreezeObject(input) {
const result = {};
for (const key in input) {
// only for non inherited keys
if (Object.hasOwn(input, key)) {
const value = input[key];
if (typeof value !== 'object') {
result[key] = Object.freeze(value);
} else {
// recursively keep sealing each level
result[key] = deepFreezeObject(value);
}
}
}
// finally seal the root
return Object.freeze(result);
}

Whether to deepFreeze or deepSeal an object really depends on what type of restrictions you want imposed on the object. It’s better to understand the difference between them, so as to make the best possible decision when it comes to preventing/restricting modifications of an object.

Further Reading

I strongly encourage you to explore and tackle additional questions in my Recursion Questions for Frontend Interviews blog series.

By doing so, you can enhance your understanding and proficiency with recursion, preparing you to excel in your upcoming frontend interviews.

Wishing you best. Happy Interviewing 🫡.