我有以下代码:如果我在JavaScript中拼接克隆的数组,为什么我的原始数组会拼接?
var coords = [
{lat: 39.57904, lng: -8.98094, type: "a"}, // A
{lat: 39.55436, lng: -8.95493, type: "b"}, // B
{lat: 39.56634, lng: -8.95836, type: "c"} // C
];
var travelingOptions = [];
getAllTravelingOptions();
function getAllTravelingOptions(){
coords.forEach((point, pos) => {
let c = coords;
delete c[pos];
console.log(c);
console.log(coords);
});
}
为什么变量c
和coords
始终是相同的?如果我在c
上删除,它反映在coords
上的操作。这是一个正常的行为?
'C'和'coords'是同一个对象都引用。 – Amy