As per the example given here,
let first:number[] = [1, 2];
let second:number[] = [3, 4];
let both_plus:number[] = [0, ...first, ...second, 5];
console.log(`both_plus is ${both_plus}`);
first[0] = 20;
console.log(`first is ${first}`);
console.log(`both_plus is ${both_plus}`);
both_plus[1]=30;
console.log(`first is ${first}`);
console.log(`both_plus is ${both_plus}`);
Spreading shows a deep copy, because all three array's have it's own duplicates, based on below output:
both_plus is 0,1,2,3,4,5
first is 20,2
both_plus is 0,1,2,3,4,5
first is 20,2
both_plus is 0,30,2,3,4,5
Documentation says: Spreading creates a shallow copy of first
and second
. How do I understand this?
[0, 1, 2, 3, 4, 5]
. You can't really tell if a copy is deep if all you've got are primitives. – Jewfish