我听到过类似的问题,但没有得到我想要的答案; 我不认为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"]);
- Pointy