Javascript 中使用 const 与 freeze 的声明

admin 阅读:109 2024-08-12

javascript 中使用 const 与 freeze 的声明

javascript 常量

使用 const 我们仍然可以修改 javascript 对象的内容,但对该对象的引用将是不可变的。

const product = {name: "sugar", weight: "1 kg"};
product.name = "some new name";

console.log(product);
{
  name: "some new name",
  weight: "1 kg"
}

javascript 冻结

当我们不想修改对象的内容时,首选使用 freeze。

const product = {name: "sugar", weight: "1 kg"};
object.freeze(product);
product.name = "some new name";

console.log(product);
{
  name: "Sugar",
  weight: "1 kg"
}
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!

搜索