Object.freeze: Immutable Objects

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!


  • 5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we’ve needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a…

  • How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo…

  • Reverse Element Order with CSS Flexbox

    CSS is becoming more and more powerful these days, almost to the point where the order of HTML elements output to the page no longer matters from a display standpoint — CSS lets you do so much that almost any layout, large or small, is possible.  Semantics…

  • Animating CSS3 Transforms with MooTools Fx

Leave Your Comment