卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章73750本站已运行433

Javascript 中使用 const 与 freeze 的声明

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"
}
卓越飞翔博客
上一篇: 如何通过验证令牌在 PHP 中设置电子邮件验证:完整指南
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏