There are many articles about deep clone on the Internet, but to be honest, as an interviewer, I think many code implementations are not qualified. Now I’ll show you how to answer this question from the perspective of the interviewer.

Ask yourself three questions before reading this:

  • Do you really understand what does deep clone means?
  • In the eyes of the interviewer, what kind of deep clone function is qualified?
  • How to write a better deep clone function?

This article takes you step-by-step through the process of creating a better deep clone function.

Definition of deep clone and shallow clone

Although deep clone has been discussed many times, many readers are still not familiar with the concept, so I will introduce it again.

Shallow clone duplicate as little as possible. A shallow clone of a collection is a copy of the collection structure, not the elements. With a shallow clone, two collections now share the individual elements.

Deep clone duplicates everything. A deep clone of a collection is two collections with all of the elements in the original collection duplicated.

Shallow clone:

Image for post

Deep Clone:

Image for post

#javascript #web development

Write a Better Deep Clone Function in JavaScript
10.15 GEEK