DEV Community

Discussion on: What's wrong with this code?

Collapse
 
sadarshannaiynar profile image
Adarsh

@joelnet I still don't need cookTurkey instead I can do turkey.cooked = true and so it doesn't matter whether I use const or let if my code is written keeping immutability in mind.

Collapse
 
joelnet profile image
JavaScript Joel

that is kind of why I mentioned '(just a note, const doesn't make object immutable, you have to freeze)' was to distinguish between const and immutable.

If you want a primitive value to be immutable, const is enough. If you want an object to be immutable, you have to freeze it as well.

So if we can assume groceries is immutable (which is should be).

const deepFreeze = require('deep-freeze')

const grocieries = deepFreeze({
  flour: {
    cooked: false
  },
  turkey: {
    cooked: false
  },
  eggs: {
    cooked: false
  }
})

Then your groceries will truly be immutable

const { turkey } = grocieries
turkey.cooked = true
//=> turkey({ cooked: false })
Thread Thread
 
sadarshannaiynar profile image
Adarsh

Sorry. Misunderstood as the freeze wasn't mentioned in the previous code. Thanks for clearing it up! :)