Javascript 中的参考值记忆 [英] Memory of Reference Values in Javascript

查看:57
本文介绍了Javascript 中的参考值记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript 将引用值存储在堆内存中,因此变量存储到一个指针/地址.因此,如果我们更改原始变量,它也会更改分配的变量:

JavaScript stores Reference Values in Heap memory and thus variables stores to a pointer/address. So, if we change original variable, it changes the assigned one as well:

var cols = ['red', 'blue', 'green']
var newCols = cols

cols.push('orange')

console.log(cols) 
// returns ["red", "blue", "green", "orange"]
console.log(newCols)
// returns ["red", "blue", "green", "orange"]

然而,当我尝试引用类型时,我发现,当我使用索引进行赋值和推送方法时,它们的行为与我没想到的逻辑不同:

However, when I experimenting for reference types, I find out that, when I am using indexing for assignment and push method they act in not same logic which I did not expect:

var cols = ['red', 'blue', 'green']
var newCols=cols[1]
cols[1] = "pink"
console.log(cols)
// returns > ["red", "pink", "green"]
console.log(newCols)
// returns > "blue"  WHY?

但是:

var cols = ['red', 'blue', 'green']
var newCols=cols
newCols[3] = 'orange'
cols.push('yellow')

console.log(cols)
// returns > ["red", "blue", "green", "orange", "yellow"]
console.log(newCols)
// returns > ["red", "blue", "green", "orange", "yellow"] WHY?

那么,这里的逻辑是什么(幕后的想法"),在一个样本中,它会影响原创,但在另一个样本中却没有?

So, what is the logic ('the idea under the hood') here that, in one sample, it affects original, but in another it does not?

推荐答案

什么时候做

var newCols=cols[1]

您基本上引用了 cols[1] 当前持有的值,并且让 newCols 也指向它.例如,假设 cols[1],当前的 'blue' 字符串位于内存位置 #5432.然后做 newCols=cols[1]newCols 也指向#5432.

you essentially take the reference to the value that cols[1] currently holds, and have newCols point to it too. For example, say cols[1], currently the 'blue' string, is at memory location #5432. Then doing newCols=cols[1] has newCols also point to #5432.

cols 的数组也是对内存位置的引用数组.所以 ['red', 'blue', 'green'] 可以被认为是 [#987, #5432, #123].重新分配其中一个索引会在该位置放置一个新参考.在 cols[1] = "pink" 之后,你可以认为 cols 现在包含 [#987, #333, #123].

The array that cols is is also an array of references to memory locations. So ['red', 'blue', 'green'] could be thought of as [#987, #5432, #123]. Reassigning one of the indicies puts a new reference at that spot. After cols[1] = "pink", you can think of cols as now containing [#987, #333, #123].

#5432 处的原始项目 - 'pink' - 不受影响.newCols,指向#5432,仍然指向'pink'.

The original item at #5432 - 'pink' - is not affected. newCols, which points to #5432, still points to 'pink'.

当你这样做

var newCols=cols

整个数组容器由两个变量指向.例如,colsnewCols 都指向#55555.因此,变异一个会导致另一个看起来也发生变化.

the whole array container is pointed to from two variables. Eg, both cols and newCols point to #55555. So mutating one will result in the other appearing to change as well.

(内存位置"是一种有用的可视化,但这不一定是它的原理,也不是事物在幕后的运作方式——这只是一种简单的思考方式)

(The "memory location" is a useful visualization, but it's not necessarily how it isn't how things work under the hood - this is just an easy way of thinking about it)

这篇关于Javascript 中的参考值记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆