Javascript - pass by sharing


Mar 18, 2024

如果用.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]






你可能感興趣的文章

Day 181

Day 181

Electric Lift Massage Tables: Elevate Your Massage Experience

Electric Lift Massage Tables: Elevate Your Massage Experience

下拉選單內容 Dropdown Menu

下拉選單內容 Dropdown Menu






留言討論