为什么可以修改myVariable?
const obj = {
a: 'a'
}
const myVariable = obj;
try{
myVariable = { a: 'c' } //The const declaration creates a read-only reference to a value
}catch(e){
console.log(e);
}
myVariable.a = 'b';
console.log(myVariable); //{a: "b"}
答案 0 :(得分:1)
之所以会这样,是因为您的常量实际上存储了对该对象的引用。在向对象添加对象时,您无需重新分配或重新声明该常量,而只是将其添加到常量所指向的“列表”中。在此处请参见:Why can I change value of a constant in javascript