设置一个变量等于另一个变量 [英] Setting a variable equal to another variable
问题描述
关于在JavaScript中将一个变量设置为与另一个变量相等,我有几个问题.
I have a few questions about setting a variable equal to another variable in JavaScript.
假设我们创建一个对象a
并设置b = a
.
Let's say we create an object, a
and set b = a
.
var a = {
fname: "Jon",
lname: "Smith",
age: 50
}
var b = a;
我知道,如果我们更改a
的属性之一,b
也将被更改,因为设置b = a
时,我们不会克隆a
的数据,而是创建对a
的引用c0>的数据.例如,如果我们设置a.fname = "Sarah"
,则b.fname
的新值将是"Sarah"
.
I understand that if we change one of a
's properties b
will also be changed because when we set b = a
we don't clone a
's data, but rather create a reference to a
's data. For example if we set a.fname = "Sarah"
, the new value of b.fname
will be "Sarah"
.
如果通过设置a = {}
尝试清除" a
,则对象b
将保持不变.我不明白为什么以这种方式操作对象会产生与第一个示例不同的结果.
If we try to "clear" a
though by setting a = {}
, object b
will remain unchanged. I don't understand why manipulating an object in this way produces a different result than in the 1st example.
我对以下情况也有疑问.
Also I have a question about the following scenario.
var x = 10;
var z = x;
如果我们随后设置x = 20
,则z
的值保持不变.基于我在第一个问题中描述的行为,人们会认为z
的新值将反映x
的新值.有人可以解释一下我在这里想念的东西吗?
If we then set x = 20
, the value of z
remains unchanged. Based on the behavior described in my 1st question, one would think that the new value of z
would reflect the new value of x
. Could someone please explain what I am missing here?
谢谢!
推荐答案
这两个问题的真正简短答案是,当您使一个变量等于另一个变量时,对第一个变量的内容进行复制,然后存储在第二个变量中-两个变量之间没有链接.
The really short answer to both your questions is that when you make one variable equal to another, a COPY of what's in the first variable is made and stored in the second variable - there is no linkage between the two variables.
但是,请继续阅读以获取更多详细信息,以及在某些情况下为什么看起来有链接...
But, read on for more details and why it can seem like there is a link in some cases...
JavaScript与许多语言一样,将数据分为两大类:值类型和引用类型. JavaScript值类型为 其基元 :
JavaScript, like many languages, divides data into two broad categories: value types and reference types. JavaScript value types are its primitives:
- 字符串
- 号码
- 布尔值
- 空
- 未定义
- 符号
将这些类型中的任何一种分配给变量时,实际数据都存储在该变量中,并且如果将一个变量设置为另一个变量,则原语的副本(而不是链接)并存储在新变量中:
When you assign any of these types to a variable, the actual data is stored in that variable and if you set one variable equal to another, a copy (not a linkage) of the primitive is made and stored in the new variable:
var a = 10; // Store the actual number 10 in the a variable
var b = a; // Store a COPY of the actual number stored in a (10) in the b variable
a = 50; // Change the actual data stored in a to 50 (no change to b here)
console.log(b); // 10
使用 引用类型时> ,情况会有所不同.将变量分配给引用类型意味着该变量仅保留对对象实际存储位置的引用,而不是对实际对象本身的引用.因此,当您执行此操作时:
When you work with reference types, something a little different happens. Assigning a variable to a reference type means that the variable only holds a reference to the memory location where the object is actually stored, not the actual object itself. So, when you do this:
var a = {foo:"bar"};
a
实际上并不存储对象本身,它仅存储可以找到对象的内存位置(即0x3C41A).
a
does not actually store the object itself, it only stores the memory location for where the object can be found (i.e. 0x3C41A).
但是,就设置另一个变量等于第一个变量而言,它仍然与原始变量一样工作--第一个变量的副本是并赋予第二个变量.
But, as far as setting another variable equal to the first goes, it still works as it did with primitives - - a copy of what's in the first variable is made and given to the second variable.
这是一个例子:
// An object is instantiated in memory and a is given the address of it (for example 0x3C41A)
var a = {};
// The contents of a (the memory location of an object) is COPIED into b.
// Now, both a and b hold the same memory location of the object (0x3C41A)
var b = a;
// Regardless of whether a or b is used, the same underlying object
// will be affected:
a.foo = "test";
console.log(b.foo); // "test"
// If one of the variables takes on a new value, it won't change
// what the other variable holds:
a = "something else";
console.log(b); // The object stored in memory location (0x3C41A)
因此,在您的第一个测试中,您仅获得了两种访问一个对象的方式,然后将a
持有的内容(该对象的存储位置)更改为另一个对象,因此现在只有一个通过b
向左访问原始对象的方式.
So, in your first tests, you've simply got two ways of accessing one object and then you change what a
is holding (the memory location of the object) to a different object and therefore now you only have one way left to access the original object, through b
.
如果我们尝试通过设置
a = {}
来清除"a
,则对象b
将保留 不变.我不明白为什么要这样处理对象 产生与第一个示例不同的结果.
If we try to "clear"
a
through by settinga = {}
, objectb
will remain unchanged. I don't understand why manipulating an object in this way produces a different result than in the 1st example.
因为现在我们知道a = {}
并未清除对象.只是将a
指向其他东西.
Because now we know that a = {}
isn't clearing the object. It's just pointing a
at something else.
这篇关于设置一个变量等于另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!