HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Object cloning is another common operation in development, especially frontend. Cloning creates a copy of the original object, i.e., a new object filled with the same data.</p>
1 <p>Object cloning is another common operation in development, especially frontend. Cloning creates a copy of the original object, i.e., a new object filled with the same data.</p>
2 <p>JavaScript has no built-in function to perform cloning, but you can emulate it using Object.assign(). To do this, just pass an empty object as the first parameter, and the one you want to clone as the second parameter:</p>
2 <p>JavaScript has no built-in function to perform cloning, but you can emulate it using Object.assign(). To do this, just pass an empty object as the first parameter, and the one you want to clone as the second parameter:</p>
3 <p>The result is two different objects with the same content. Since they are different, changes in one do not change the data in the other.</p>
3 <p>The result is two different objects with the same content. Since they are different, changes in one do not change the data in the other.</p>
4 <p>Cloning is also performed using the<a>clone()</a>function from the<em>lodash</em>library. Although its result is identical to the examples above, it has better operation semantics due to its name.</p>
4 <p>Cloning is also performed using the<a>clone()</a>function from the<em>lodash</em>library. Although its result is identical to the examples above, it has better operation semantics due to its name.</p>
5 <p>Cloning with the methods above does not affect nested objects. They end up in the new object from the reference stored in the old one.</p>
5 <p>Cloning with the methods above does not affect nested objects. They end up in the new object from the reference stored in the old one.</p>
6 <p>This cloning is called<em>shallow</em>cloning. It's very important to remember that this is what JavaScript means when it uses the term "cloning". This is not a bad thing, shallow cloning is handy for many tasks. But if isn't enough, you have to use deep cloning instead.</p>
6 <p>This cloning is called<em>shallow</em>cloning. It's very important to remember that this is what JavaScript means when it uses the term "cloning". This is not a bad thing, shallow cloning is handy for many tasks. But if isn't enough, you have to use deep cloning instead.</p>
7 <p>There is no built-in way in JavaScript to clone objects completely. Therefore, it's customary for developers to use the ready-made<a>cloneDeep()</a>function from the<em>lodash</em>library.</p>
7 <p>There is no built-in way in JavaScript to clone objects completely. Therefore, it's customary for developers to use the ready-made<a>cloneDeep()</a>function from the<em>lodash</em>library.</p>
8 <p>Deep cloning has one serious disadvantage. If the object is large and has a complex structure deep cloning can have a big impact on performance, and this is why it's not a default method.</p>
8 <p>Deep cloning has one serious disadvantage. If the object is large and has a complex structure deep cloning can have a big impact on performance, and this is why it's not a default method.</p>