【发布时间】:2018-10-07 15:14:28
【问题描述】:
按照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 显示一个深拷贝,因为所有三个数组都有自己的重复项,基于以下输出:
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 说:传播会创建 first 和 second 的浅表副本。我怎么理解这个?
【问题讨论】:
-
同
[0, 1, 2, 3, 4, 5]。如果您所拥有的只是原始数据,您将无法真正判断副本是否深。 -
不,传播语法不会创建或复制任何内容。创建新数组的是 数组字面量。
标签: typescript ecmascript-6 deep-copy shallow-copy