为什么数字在Javascript中是不可变的? [英] Why number are immutable in Javascript?

查看:108
本文介绍了为什么数字在Javascript中是不可变的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里已经阅读了问题和答案:

I have read the question and answer here:

javascript numbers- immutable

但是我不清楚为什么数字(原始类型)是不可变的?只是因为他们创建了一个新的引用但没有覆盖这个值?

But it's not enough clear for me why the number (primitive type) are immutable? Just because they create a new reference but not overwrite the value?

如果在每个assignemt上创建一个新的引用

If on each assignemt is created a new reference

var x = 5;
x = 1;

我们在以下循环中有100次新参考吗?

Would we have 100 times a new reference in the following loop?

while (x < 101)
{
    x++;
}

效率这么高吗?我想我没看错。

Is that efficient? I think I am not seeing correctly.

推荐答案

我真的不太确定你所期待的答案是什么,因为我不喜欢我非常明白你的困惑。但是我们走了:

I'm honestly not quite sure what kind of answer you expect since I don't quite understand what you are confused about. But here we go:


我们在以下循环中有100次新引用吗?

Would we have 100 times a new reference in the following loop?

变量只是值的容器。在低级别,变量基本上只是存储器地址或寄存器的标签。例如。变量 x 可能指向注册 R1

Variables are just containers for values. At a low level a variable is basically just a label for a memory address or a register. E.g. variable x might point to register R1.

x ++ 只会通过 1 增加该寄存器中存储的数字。让我们假设我们的注册看起来像这样:

x++ would simply increment the number that is stored in that register by 1. Lets assume our register looked like this:

R1: 5

递增后,可以是单个操作,例如 ADD R1 1 ,我们会得到

After incrementing it, which can be a single operation, such as ADD R1 1, we would get

R1: 6

即我们简单地用新的值覆盖了以前的值。我们多次这样做。

I.e. we simple overwrote the previous value with a new one. And we do that multiple times.


效率这么高吗?我认为我没有正确看到。

Is that efficient? I think I am not seeing correctly.

将数字递增1就像操作一样简单。

Incrementing a number by one is as simple of an operation as it can get.

当然,你可以在更高的层次上实现可变数字,但它肯定不会使事情更有效或更简单。

Sure, you could implement mutable numbers on a higher level, but it certainly wouldn't make things more efficient or simpler.

可变性对单值值没有多大意义,因为改变这样的值基本上意味着用就地替换它不同的值。

Mutability doesn't make much sense for "single value" values, because mutating such a value basically means replacing it with a different value "in place".

可变性对于由列表和字典等其他值组成的值更有意义,其中一个部分发生变化而另一个部分保持不变。

Mutability makes more sense for values that are composed of other values such as lists and dictionaries, where one part changes and the other stays the same.

此外,可变性似乎只与语言具有引用类型数据类型。我的意思是,多个变量可以保存对数据类型的相同值的引用。对象在JavaScript中是引用类型,允许您这样做:

Additionally, mutability only seems relevant when a language has reference type data types. With that I mean that multiple variables can hold a reference to the very same value of a data type. Objects are reference-type in JavaScript, which allows you to do this:

var a = {foo: 42};
var b = a;
b.foo = 21;
console.log(a);

如果数据类型不是引用类型,称为value-type,(原始值在JavaScript中),则可变性无关紧要,因为它无法区分从不变性。考虑以下具有可变值值类型的假设场景:

If data types are not of a reference-type, called value-type, (which primitive values are in JavaScript), then mutability doesn't matter because it would be indistinguishable from immutability. Consider the following hypothetical scenario with a mutable, value-type number:

var a = MutableNumber(42);
var b = a; // creates a copy of MutableNumber(42) because it's a value type
a.add(1);
console.log(a, b); // would log 43, 42

在这种情况下,两个变量不可能引用相同的可变数值, a.add(1)与向 a 分配新值无法区分(即 a = a + 1 )。

In this scenario it is not possible for two variables to refer to the same mutable number value, a.add(1) is indistinguishable from assigning a new value to a (i.e. a = a + 1).

这篇关于为什么数字在Javascript中是不可变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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