我听过类似的问题,但没有得到我想要的答案;
我不计算 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);
只需使用Object.freeze
const immutableArray = Object.freeze([1,2,4])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)