如果用.push等操作,會影響原始值;如果是直接賦予新值,不會影響原始值
1.
function modifyArray(arr) {
arr.push(4)
}
let myArray = [1, 2, 3]
modifyArray(myArray)
console.log(myArray) // [1, 2, 3, 4]
2.
function modifyArray(arr) {
arr = [1, 2, 3, 4]
}
let myArray = [1, 2, 3]
modifyArray(myArray)
console.log(myArray) // [1, 2, 3]