Javascript - pass by sharing


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






你可能感興趣的文章

array 類型的內建函式

array 類型的內建函式

[ Vue3 ] 筆記 ref、reactive

[ Vue3 ] 筆記 ref、reactive

SQL Table Value Constructor – SELECT Statement (Create a Table With Value Generated By My Self)

SQL Table Value Constructor – SELECT Statement (Create a Table With Value Generated By My Self)






留言討論