112k views
1 vote
Are object references maintained after slice() on the shallow copy?

A. Object references are deep-copied when using slice().
B. slice() creates a brand new set of objects, unrelated to the original array's objects.
C. Changes to objects within the copied array will affect the objects in the original array.
D. The object references in the copied array remain the same as in the original array.

1 Answer

6 votes

Final answer:

The slice() method maintains object references in the shallow copy it creates. Modifying objects in the copied array will affect the original array because both arrays reference the same objects.

Step-by-step explanation:

When you use the slice() method on an array, the answer to your question is D. The object references in the copied array remain the same as in the original array. Object references are maintained when creating a shallow copy of an array with slice(). That means if you have an array of objects and you create a new array using slice(), changes to the objects in the new array will affect the objects in the original array because they are the same references.

For example, if you have an array originalArray = [{a: 1}, {b: 2}] and you make a slice to create copiedArray = originalArray.slice(), modifying copiedArray[0].a = 3 will also change originalArray[0].a because both arrays refer to the same object.

User Sferik
by
8.2k points