Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I need to freeze an object in JavaScript?

It is not clear to me when anyone would need to use Object.freeze in JavaScript. MDN and MSDN don't give real life examples when it is useful. I get it that an attempt to change such an object at runtime means a crash. The question is rather, when would I appreciate this crash?

To me the immutability is a design time constraint which is supposed to be guaranteed by the type checker.

So is there any point in having a runtime crash in a dynamically typed language, besides detecting a violation better later than never?

like image 435
Trident D'Gao Avatar asked Feb 09 '13 20:02

Trident D'Gao


People also ask

Why do we need object freeze?

Object.freeze() MethodFreezing an object does not allow new properties to be added to an object and prevents from removing or altering the existing properties. Object. freeze() preserves the enumerability, configurability, writability and the prototype of the object.

What is the difference between const and object freeze?

Object. freeze works on values, and more specifically, object values. It makes an object immutable, i.e. you cannot change its properties. Basically, const is the new var ; it's just block-scoped and prevents reassignment.

How do you unfreeze an object in JavaScript?

There is no way to do this, once an object has been frozen there is no way to unfreeze it. This is technically correct as far as mutating the existing object. However, you can copy/clone the existing object and mutate it's properties now.

How can we make an object immutable in JavaScript?

You can freeze (make immutable) an object using the function Object. freeze(obj) . The object passed to the freeze method will become immutable. The freeze() method also returns the same object.


2 Answers

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.

like image 153
Ian Atkin Avatar answered Oct 07 '22 19:10

Ian Atkin


You can use this when you have an object representing a logically immutable data structure, especially if:

  • Changing the properties of the object or altering its "duck type" could lead to bad behavior elsewhere in your application
  • The object is similar to a mutable type or otherwise looks mutable, and you want programmers to be warned on attempting to change it rather than obtain undefined behavior.

As an API author, this may be exactly the behavior you want. For example, you may have an internally cached structure that represents a canonical server response that you provide to the user of your API by reference but still use internally for a variety of purposes. Your users can reference this structure, but altering it may result in your API having undefined behavior. In this case, you want an exception to be thrown if your users attempt to modify it.

like image 42
Plynx Avatar answered Oct 07 '22 19:10

Plynx