When you make something const
in JavaScript, you can't reassign the variable itself to reference something else. However, the variable can still reference a mutable object.
const x = {a: 123};
// This is not allowed. This would reassign `x` itself to refer to a
// different object.
x = {b: 456};
// This, however, is allowed. This would mutate the object `x` refers to,
// but `x` itself hasn't been reassigned to refer to something else.
x.a = 456;
In the case of primitives such as strings and numbers, const
is simpler to understand, since you don't mutate the values but instead assign a new value to the variable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…