Object.freeze: Immutable Objects

By  on  

One of my favorite part of JavaScript has always been mutability of objects.  I loved that MooTools and likewise frameworks could modify native prototypes to enhance them with functionality we knew the language need; in fact, I credit MooTools with pushing the web forward.

There are cases, however, where you don't want an object to be modifiable; you don't want values for existing properties to be changed, added, or removed.  That's where Object.freeze can help -- with Object.freeze you can create immutable objects you can trust!

const obj = Object.freeze({
    x: 1,
    y: 2
});

// None of these do anything
obj.x = 8; // { x: 1, y: 2}
delete obj.x; // { x: 1, y: 2}
obj.z = 3;  // { x: 1, y: 2}

Object.freeze is a welcomed addition to JavaScript and a necessary one.  If you have objects whose integrity you want to secure, immutability is required.  Object.seal provides similar functionality without the ability to freeze values, so Object.freeze is your best choice when you want to lock down an object!

Recent Features

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • By
    Generate Dojo GFX Drawings from SVG Files

    One of the most awesome parts of the Dojo / Dijit / DojoX family is the amazing GFX library.  GFX lives within the dojox.gfx namespace and provides the foundation of Dojo's charting, drawing, and sketch libraries.  GFX allows you to create vector graphics (SVG, VML...

  • By
    Introducing LazyLoad 2.0

    While improvements in browsers means more cool APIs for us to play with, it also means we need to maintain existing code.  With Firefox 4's release came news that my MooTools LazyLoad plugin was not intercepting image loading -- the images were loading regardless of...

Discussion

  1. Shallow

    It’s good to remind that Object.freeze does only shallow freeze.

  2. That’s a nice feature. Much needed. However, I hope the browser support is good.

  3. MaxArt

    Should I remind that MooTools’ practice to extend native prototypes prevented W3C to use well-known names like contains and flatten, because it would have literally broken the web?
    Extending native prototypes is the last thing you’d want in a general purpose library.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!