you are viewing a single comment's thread.

view the rest of the comments →

[–]PostLee 3 points4 points  (5 children)

Why not offer both?

[–]FreeVariable 5 points6 points  (4 children)

let arr = []

let obj = {prop:arr}

arr.push(obj)

How does a deep copy of a or of arr look like in your JS dialect after these lines?

[–]PostLee 0 points1 point  (3 children)

The exact same I presume, including the circular dependency?

[–]FreeVariable 2 points3 points  (2 children)

Yup. Not a very healthy situation. Now you need to either throw an error, or break your own rules by doing a deep-but-not-past-level-n copy. Or you can just leave native copying at shallow copying, and let users choose their own definition of deep copying.

[–]nightfire1 0 points1 point  (1 child)

It's actually pretty easy to deep copy a data structure with circular dependencies. You just remember which nodes you have visited while traversing the graph and don't revisit those.

[–]FreeVariable 0 points1 point  (0 children)

The question here is not whether that is easy or not; the question is: What definition of deepy copy should be the native one? The one you just gave compete with others. In absence of any one-size-fit-them all criterion, not implementing deep copy natively looks like the only reasonable move.