Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2072
Abstract
idth="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="8a3b">The above situation might be valid in certain use-cases and one might want to modify the source objects. But more often than not, this is something that might have occurred by mistake and is going to lead to trouble.</p><p id="cd34">Assume the scenario illustrated above, in a complex web application where the reference might be available at multiple different parts of the code spanning through various conditional flows. Just that assumption alone, should have given the shivers, didn’t it?</p><p id="12eb">It is always a best practice to not mutate the source, rather make a copy and modify it, if one has to.</p><h2 id="f118">Freezing Objects:</h2><p id="1774">It is as simple as calling the <code>Object.freeze()</code>method and supplying the object instance to be frozen, as the parameter.</p><p id="d4b1">Our example snippet could be modified as follows:</p>
<figure id="24b7">
<div>
<div>
<iframe class="gist-iframe" src="/gist/Parthipan-Natkunam/44fa1606501f0e66ad6dde459d73bd62.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="815f">Now, this code will throw an “<i>object is not extensible” </i>runtime error because we are trying to mutate frozen objects.</p><p id="77bb">With just a line of code, we can enforce the best practice and at the same time prevent potential bugs. Doesn’t that sound like a double win?</p><h2 id="8007">Limitation of Freeze:</h2><p id="d31a">So far we have just looked at simple objects. But as developers, we are very much aware that nested objects are a reality that could be often spotted in codebases.</p><p id="6acf">If one has tried the above technique on a nested object, they might have figured out that it doesn’t work quite as expected.</p>
<figure id="1343">
<div>
<div>
Options
<iframe class="gist-iframe" src="/gist/Parthipan-Natkunam/6532ddb905ebc4dd27072f7950f04c67.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="1e1c">From the code above, we can infer that freeze is shallow.</p><h2 id="e7a5">Deep-freeze Objects:</h2><p id="6b30">The limitation we see above could be easily overcome through recursively freezing the nested object.</p>
<figure id="2f28">
<div>
<div>
<iframe class="gist-iframe" src="/gist/Parthipan-Natkunam/62eed3635f1ac6364f5d696d5a62f509.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="27d7">Now we can just call <code>deepFreeze(nestedObject)</code>instead <code>Object.freeze(nestedObject)</code> and it would work as expected.</p><p id="3c87"><b><i>Word of caution: </i></b><i>This will end up in an infinite loop if called on cyclic references. This is a fact that one should be aware of while deep-freezing objects.</i></p><h2 id="e4c3">Conclusion:</h2><p id="d31d">In this article, we have taken a closer look at the freeze method and its limitation. We have also experimented with a recursive approach to overcome the limitation.</p><p id="a68e">However, the reader should be aware that the <code>deepFreeze</code>method, as seen in the code is recursive and if used on a complex object with multiple levels of nesting, might cause the stack to overflow.</p><p id="2964">This piece of writing is aimed to give the reader the underlying principle of immutability and how to achieve it without a third-party library. But if you are to use this concept in a production code you might be better off with libraries like <a href="https://immutable-js.github.io/immutable-js/"><i>immutable JS</i></a><i>.</i></p><p id="73fc">Thanks for reading my thoughts, cheers :)</p></article></body>