题目如下:
var foo = { bar: 1 }
var arr1 = [1, 2, foo]
var arr2 = arr1.slice(1)
arr2[0]++
arr2[1].bar++
foo.bar++
arr1[2].bar++
console.log(arr1[1] === arr2[0])
console.log(arr1[2] === arr2[1])
console.log(foo.bar)
依次分析
初始
foo:A
arr1:B
arr2:C
A:{bar:1}
B:[1,2,A]
C:[2,A]
arr2[0]++
A:{bar:1}
B:[1,2,A]
C:[3,A]
arr2[1].bar++
A:{bar:2}
B:[1,2,A]
C:[3,A]
foo.bar++
A:{bar:3}
B:[1,2,A]
C:[3,A]
arr1[2].bar++
A:{bar:4}
B:[1,2,A]
C:[3,A]
所以
console.log(arr1[1] === arr2[0]) // false
console.log(arr1[2] === arr2[1]) // true
console.log(foo.bar) // 4