javascript数字 - 不可变的 [英] javascript numbers- immutable

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

问题描述

我来自c#background,其中使用public get,private set属性实现了immutable。
我已经读过javascript中的数字是不可变的,所以我该如何做以下

I come from c# background where immutable is achieved with public get ,private set properties. I have read that numbers in javascript are immutable so how can I do the following

var x = 6 / 2;
console.log(x);  // 3
 x = 8;
console.log(x); // 8

我改变了x,我认为我不能?

I have changed x, which I thought I couldn't?

推荐答案

数字本身是不可变的。存储在变量中的对它们的引用不是。

The numbers themselves are immutable. The references to them that are stored in the variable are not.

所以 6/2 获取对不可变的 3 ,然后 = 8 为不可变的 8分配一个新的引用

So 6 / 2 gets you a reference to the immutable 3, and then = 8 assigns a new reference to the immutable 8.

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

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