The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Immutable.js: Introduction - Easing the Pains of Mutability

Immutable.js: Introduction - Easing the Pains of Mutability

3:22
By utilizing immutable data structures, we can write code that is easier to reason about, avoid mutation-related bugs, reduce complexity, and even gain some performance benefits along the way. Before diving too far into Facebook's [Immutable.js](https://facebook.github.io/immutable-js/) library, let's take a moment to examine some of the pains and obstacles that mutable data structures present. We'll take a brief glimpse at putting the Immutable "List" data structure to work by example. We will convert our usage of Javascript's native `Array` to use [Immutable.List](https://facebook.github.io/immutable-js/docs/#/List) in order to address a mutation-related "bug" caused by multiple objects "sharing" a mutable structure (array) by reference.
Watch this lesson now
Avatar
egghead.io

By utilizing immutable data structures, we can write code that is easier to reason about, avoid mutation-related bugs, reduce complexity, and even gain some performance benefits along the way.

Before diving too far into Facebook's Immutable.js library, let's take a moment to examine some of the pains and obstacles that mutable data structures present.

We'll take a brief glimpse at putting the Immutable "List" data structure to work by example. We will convert our usage of Javascript's native Array to use Immutable.List in order to address a mutation-related "bug" caused by multiple objects "sharing" a mutable structure (array) by reference.

Avatar
Nicholas

Great stuff. More lessons on Immutable.js please.

In reply to egghead.io
Avatar
dave

I'm confused by your example , surely if you put
let guest = name => ({
name,
foods: [ ],
eat(food) {
this.foods.push(food);
return this;
}
});

everything is OK, why do you set guest.food to default food in the first place ?

In reply to egghead.io
Avatar
Igor

Actually to completely reflect behaviour of example you just need to change one line :)
foods: defaultMenu,
to
foods: [].concat(defaultMenu)

In reply to egghead.io
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?