Object.freeze()与const [英] Object.freeze() vs const
问题描述
Object.freeze()
似乎就像过渡便利方法要在ES6中使用const
一样.
在某些情况下两者都在代码中占据了位置吗?还是有一种首选的方式来处理不可变数据?
Are there cases where both take their place in the code or is there a preferred way to work with immutable data?
我应该使用 Object.freeze()
直到我使用的所有浏览器都支持const
的那一刻,然后切换为使用const
?
Should I use Object.freeze()
until the moment all browsers I work with support const
then switch to using const
instead?
推荐答案
const
和Object.freeze
是两个完全不同的东西.
const
and Object.freeze
are two completely different things.
const
适用于绑定(变量").它会创建一个不可变的绑定,即您不能为该绑定分配新的值.
const
applies to bindings ("variables"). It creates an immutable binding, i.e. you cannot assign a new value to the binding.
Object.freeze
适用于值,更具体地说,适用于对象值.它使对象不可变,即您无法更改其属性.
Object.freeze
works on values, and more specifically, object values. It makes an object immutable, i.e. you cannot change its properties.
这篇关于Object.freeze()与const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!