2 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
数组等引用类型变量存储的是地址,上边那种相当于直接改变了该变量的地址指向,就和你直接重新赋值变量是一个概念。所以会报错。
下边那种改变的是数组内的元素,本质上names存储的内存地址并未改变,所以不会报错
回首忆惘然
TA贡献1847条经验 获得超11个赞
:The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.
具体解释就是,变量在编译链接后会被分配到具体的某个内存地址,变量值是该地址存储的内容。用const初始化变量,就是将初始变量值分配到指定内存地址。此时内存地址已经确定不能变,再赋值会将这个变量分配到别的地址,报错;而地址中存储的变量值可以被修改。
添加回答
举报
0/150
提交
取消
