Why would I need to freeze an object in JavaScript?

The Object.freeze function does the following:

  • Makes the object non-extensible, so that new properties cannot be added to it.
  • Sets the configurable attribute to false for all properties of the object. When – configurable is false, the property attributes cannot be changed and the property cannot be deleted.
  • Sets the writable attribute to false for all data properties of the object. When writable is false, the data property value cannot be changed.

That’s the what part, but why would anyone do this?

Well, in the object-oriented paradigm, the notion exists that an existing API contains certain elements that are not intended to be extended, modified, or re-used outside of their current context. The final keyword in various languages is the most suitable analogy of this. Even in languages that are not compiled and therefore easily modified, it still exists, i.e. PHP, and in this case, JavaScript.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)