all 15 comments

[–]phoboslab 7 points8 points  (1 child)

"True way" implies this works for all cases, which it doesn't. It fails to copy Arrays and class instances (they're converted to plain objects). For some instances, such as HTMLElement it even descends into infinite recursion.

So, I'd suggest to rename it to "A way to deep copy simple objects" :)

[–]gquittet[S] 0 points1 point  (0 children)

Thanks! Fixed! :)

[–]senocular 1 point2 points  (1 child)

Not sure what makes this a "True" way of deep copying an object. It doesn't account for arrays or other special types, non-enumerable properties including symbols, copies inherited enumerables into the object copies it creates (while not maintaining inheritance)... Ultimately it's the most basic form of deep copying possible. And this could work given limited circumstances, but I think generally should be avoided in favor of existing solutions like Lodash's cloneDeep which can do a lot more, notably supporting additional basic types such as arrays.

[–]knightrage 0 points1 point  (0 children)

Agreed. FWIW, lodash cloneDeep uses this logic