提问人:GWorking 提问时间:5/29/2020 更新时间:5/29/2020 访问量:165
JS,当一个变量被赋值给另一个变量时,它是否包含对该变量的引用或该变量在那一刻所持有的值?[复制]
JS, when a variable is assigned to another variable, does it hold a reference to that variable or the value that that variable holds at that moment? [duplicate]
问:
提供此代码
let myVar = 'hey'
let myVar2 = myVar
myVar = 'what'
当我分配 时,我正在分配对 的引用?或者在分配中得到解决,我正在分配当时具有的值?let myVar2 = myVar
myVar2
myVar
myVar
我怀疑是否
myVar2
保留对原始文件的引用(由于它已被第二次分配覆盖,因此不再可用myVar
myVar = 'what'
)- 或从一开始就保持任何值,无论该变量稍后将被覆盖
myVar2
myVar
答: 暂无答案
评论
myVar
myVar2
myVar = {a: 1}; myVar2 = myVar; console.log(myVar === myVar2) /* true */; myVar2.b = 2; console.log(myVar) /* {a: 1, b: 2} */; myVar2 = 'abc'; console.log(myVar) /* {a: 1, b: 2} */;
undefined
window.undefined