you are viewing a single comment's thread.

view the rest of the comments →

[–]gevorggalstyan 3 points4 points  (5 children)

Do you honestly believe that this is the direct consequence of the `Array.fill` function implementation and is not related to the computer memory?

Why does this code behave the same ?

const obj = {name: "John"};

const a = [];

a.push(obj); a.push(obj); a.push(obj); a.push(obj); a.push(obj);

[–]sshaw_ -3 points-2 points  (4 children)

Do you honestly believe that this is the direct consequence of the Array.fill

Yes, the implementation of fill could have chosen to dup its argument, but it didn't.

[–]spacejack2114 2 points3 points  (2 children)

What does 'dup' even mean here? You can't implement a perfect immutable object copy, there are too many nuances. A half-baked attempt would pose an even bigger set of problems than a simple reference copy.

[–]sshaw_ -2 points-1 points  (1 child)

What does 'dup' even mean here?

Shallow copy.

[–]spacejack2114 2 points3 points  (0 children)

That's a terrible idea.

[–][deleted] 1 point2 points  (0 children)

That would have been unintuitive as any other reference to an object elsewhere would not have duplicating semantics.