DEV Community

Discussion on: JavaScript Interviews: Create a deep copy of an object

Collapse
 
alohci profile image
Nicholas Stimpson • Edited
let a = {};
let b = { foo: a };
a.bar = b;
let c = copyObject(b);
Enter fullscreen mode Exit fullscreen mode

Watch out for circular references in your recursion.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Preventing endless recursions in the case of circular references can be a bit trickier, specially if you want to reconstruct those same circular structures in the copy of the object. Definitely a good follow-up question, but not something I'd normally expect to be answered flawlessly during an interview, more of a "how'd you go about this?" sort of question.

EDIT: Definitely something I'd expect someone to notice, either in the process of coming up with a simple solution (aka. asking whether loops need to be considered), or at least when asked to find possible flaws in their solution.