【发布时间】:2019-07-23 20:14:16
【问题描述】:
我听说过类似的问题,但不是我想要的答案; 我不算 const 因为: 1)。 它实际上并没有使它不可变,它只是使引用不可变 2)。 它与范围混淆,我也希望它在块外工作 3)。 并非所有浏览器都支持它
{
const hello = ["hello", "world"];
hello.push("!!!");
console.log(hello);//outputs "hello", "world", "!!!"
}
//and it doesn't, and shouldn't, work here
console.log(hello);
【问题讨论】:
-
const hello = Object.freeze(["hello", "world"]);
标签: javascript immutability var