Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Make Your Objects Unchangeable with Object.freeze()
Liz Lam
Liz Lam

Posted on

     

Make Your Objects Unchangeable with Object.freeze()

One of the things that confused me initially during my JavaScript learning journey was theconst keyword. I thought it was akin toconst in C++ where the declared variable would be immutable.

This is not the case in JavaScript.

Theconst keyword does not create immutability as the name would imply (i.e. no changes allowed), but actually prevents reassignment.

So how do you make JavaScript objects immutable?

That's whereObject.freeze() comes in. It is a way tofreeze an object and as it implies, a frozen object can not be changed.

Let's look at an example in our browser console.

>> const myinfo = { name: "Liz", title: "Frontend Engineer" }

Now let's change my title via property access:

>> myinfo.title = "Elephant Trainer">> myinfoObject { name: "Liz", title: "Elephant Trainer" }

No problem, as long as we not reassigning anything tomyinfo, myname andtitle are mutable for all to change.

Let's say we do something like this:

>> const myinfo2 = Object.freeze(myinfo)

Object.freeze(myinfo) returns the frozen version ofmyinfo tomyinfo2.

Now before you break out into a song of "Let it Go", it might not be totally obvious thatmyinfo2 is frozen.

myinfo2.title = "Ballet Dancer" doesn't throw any errors but upon inspection, you will see nothing has changed.

>> myinfo2Object { name: "Liz", title: "Elephant Trainer" }

In conclusion, if you are looking for a way to make objects unchangeable in JavaScript,const is not the keyword your are looking for. UseObject.freeze() for your immutability needs.

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
gabbersepp profile image
Josef Biehler
I am a tall (1,95m) coding & drawing enthusiast that likes all type of coding and drawing cartoons. I like to work (coding & drawing) on the go with my surface#cypress #js #csharp
  • Location
    Weiden, Germany
  • Education
    Bachelor of Science
  • Joined

"final" in Java does not guarantee immutability neither. ;-) Isn't it equal to Javascript's const?

CollapseExpand
 
grepliz profile image
Liz Lam
I love open source, coffee and tinkering.
  • Location
    Oakland
  • Work
    Software Engineer
  • Joined

That is a good point. I was thinking that my example was more nuanced than I stated sincefinal behaves differently with different contexts (i.e. variable vs. method vs. class). I think I will update that portion. Thanks!

CollapseExpand
 
thecomputeriswrong profile image
leastofthese23
I write code in between emails
  • Education
    University of Phoenix
  • Work
    Full Stack Developer
  • Joined

Thanks Liz, I had the same confusion coming from C#.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I love open source, coffee and tinkering.
  • Location
    Oakland
  • Work
    Software Engineer
  • Joined

More fromLiz Lam

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp