重新分配,变异,引用类型和值类型 [英] Reassignment, mutation, reference types, and value types

查看:150
本文介绍了重新分配,变异,引用类型和值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何恰当地解释为什么这两个例子不同?​​

How would you appropriately explain why these two examples differ?

// Reassignment
let a = 1;
let b = a;
a = 2;
console.log(b); // → 1

// Mutation
let myArray = [1, 2, 3];
let ourArray = myArray;
ourArray[2] = 10;
console.log(myArray); // → [1, 2, 10]

关于为什么变量标签之间的行为不同二,许多资源声称这是因为引用类型和值类型之间的差异。但是,这不仅仅适用于之间的差异吗?

In regards to why the variable label behaves differently between the two, many resources claim that it's because of the difference between reference types and value types. But, doesn't that only apply to the differences between the values themselves?

当明确地谈论变量标签时(即为什么第一个例子中的标签不跟踪值的变化,而在第二个例子中它是这样),真正的区别与变异与重新分配有关吗?

When explicitly talking about just the variable label (i.e. why the labels in the first example do not track changes to the value, whereas it does in the second example), doesn't the true difference have to do with mutation vs. reassignment?

毕竟,我们可以轻松地重新分配myArray或myArray和它们都不会有任何持续的联系。并且,如果原始值在JavaScript中理论上是可变的,则可以使第一个示例与第二个示例相同。

After all, we could just as easily reassign either myArray or ourArray and neither of them would have any sort of ongoing link. And, if primitive values were theoretically mutable in JavaScript, the first example could be made to behave just like the second.

我还阅读了有关原始值是否仅在内存中存在一次的冲突信息。例如,如果第一个在内存中只存在一次,那么将变量标签可视化为名称标签是合适的,每个引用第一个的名称标签被卡住到相同的第一个,对吗?

I've also read conflicting information on whether or not primitive values only exist once in memory. If the number one, for example, only exists once in memory, then it would be appropriate to visualize variable labels as nametags, with every nametag that references the number one being 'stuck' to the same number one, right?

同样,如果第一个能够被变异,那么每个名称标签将跟踪对该第一个的相同变化。但由于一个是不可变的,因此名称标记可以继续引用相同的数字,也可以将它们取下并粘贴到不同的值(重新分配)。

Again, if the number one were able to be mutated, then every nametag would track the same changes to that number one. But since one is immutable, nametags can either continue referencing the same number one, or they can be taken off and stuck to a different value (reassigned).

我在这里是正确的轨道?

Am I on the right track here?

推荐答案

您将实施细节(在内存中存在一次)与评估策略(参考类型和您的问题中的值类型)和特定表达式(变异与重新分配)。各种浏览器的实施细节会有所不同,并且将来可能会发生变化。

You mix implementation details ("exist once in memory") with evaluation strategies ("reference types and value types") and specific expressions ("mutation vs. reassignment") in your question. Implementation details vary among various browsers and are likely to change in the future.

Javascript中有两种评估策略:

There are two evaluation strategies in Javascript:


  • 按值复制(调用函数时按值传递)

  • 按引用复制(调用函数时按引用传递)

值类型

值类型始终按值复制。假设字符串在Javascript中是可变的:

Value types are always copied by value. Assumed strings were mutable in Javascript:

let s = "abc";
let t = s;

s[0] = "x";
s; // "xbc";
t; // "abc";

基于值类型的变量(或标识符或名称绑定)直接包含它们的值。如果这样的可变字符串发生了变异,那么只会影响相应标识符的值。

Variables (or identifiers or name bindings) that are based on value types directly contain their values. If such a mutable string is mutated, then only the value of the corresponding identifier would be affected.

与字符串不同,数字是原子的。对于原子原语,突变和重新分配没有实际区别:

Unlike strings, numbers are atomic. For atomic primitives there is no practical distinction of mutation and reassignment:

let n = 0;
n = 1, n++, n = n + 1; // all reassignments

参考类型

包含引用类型的变量仅包含对此类型的引用,这是Javascript中的复杂值(对象)。如果两个变量引用同一个对象,它们每个都包含此引用的副本,而不是直接别名。因此,如果重新分配两个变量中的一个,这不会影响另一个。

A variable holding a reference type contains merely the reference to this type, which is a complex value (object) in Javascript. If two variables reference the same object, they each hold copies of this reference, not an direct alias. Hence, if one of the two variables is reassigned, this doesn't affect the other.

引用类型具有标识,即它们的标识不再与值绑定:

Reference types have identity, that is, their identity is no longer tied to values:

let xs = [1];
xs === [1]; // false

这篇关于重新分配,变异,引用类型和值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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