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.
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.
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().
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 🫡.