User Tools

Site Tools


immutable.js_introduction

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

immutable.js_introduction [2018/09/10 23:16]
119.94.96.186 created
immutable.js_introduction [2018/09/10 23:26] (current)
119.94.96.186
Line 4: Line 4:
  
 ===Why use Immutable?​=== ===Why use Immutable?​===
 +
 +1. Getting a nested value from an object without checking if it exists
 +
 +<​code>​
 +//in normal Javascript
 +
 +//define data
 +const data = { my: { nested: { name: '​Will'​ } } };
 +
 +//this data exists
 +const goodName = data.my.nested.name;​
 +console.log(goodName);​ // prints Will
 +
 +//this does not, and throws an error
 +const badName = data.my.lovely.name;​
 +// throws error: '​Cannot read name of undefined'​
 +
 +
 +//in Immutable
 +
 +//define data
 +const data = fromJS({ my: { nested: { name: '​Will'​ } } });
 +
 +//this data exists
 +const goodName = data.getIn(['​my',​ '​nested',​ '​name'​]);​
 +console.log(goodName);​ // prints Will
 +
 +//no such data, but no error
 +const badName = data.getIn(['​my',​ '​lovely',​ '​name'​]);​
 +console.log(badName);​ // prints undefined - no error thrown
 +</​code>​
 +
 +2. Chaining manipulations - allows you to do this:
 +
 +<​code>​
 +const pets = List(['​cat',​ '​dog'​]);​
 +const finalPets = pets.push('​goldfish'​).push('​tortoise'​);​
 +</​code>​
 +
 +3. Immutable data - Well of course =D
 +
 +Also:
  
 Your data structures change predictably Your data structures change predictably
immutable.js_introduction.txt ยท Last modified: 2018/09/10 23:26 by 119.94.96.186