我想知道为什么
let a = [1, 7, 7];
console.log(a);
function foo(array) {
array[2] = 999;
}
foo(a);
console.log(a);
打印出
[1, 7, 7]
[1, 7, 999]
符合预期
虽然代码相似,但具有2D数组
let a = [[1, 7, 7], [1, 8, 5], [1, 2, 3]];
console.log(a);
function foo(array) {
array[2][2] = 999;
}
foo(a);
console.log(a);
在调用函数之前打印出更改后的数组
[[1, 7, 7]
[1, 8, 5]
[1, 2, 999]]
[[1, 7, 7]
[1, 8, 5]
[1, 2, 999]]