Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practically Immutable

sporto
March 28, 2015

Practically Immutable

Immutable data in practice in JavaScript

sporto

March 28, 2015
Tweet

More Decks by sporto

Other Decks in Technology

Transcript

  1. var array1 = [1, 2].immutable var array2 = array1.push(3) !

    array1 //=> [1, 2] array2 //=> [1, 2, 3]
  2. Predictability Store Comp Comp Data Data wants to sort data

    wants to transform data doesn't want its data to be modified externally
  3. • In JS strings and numbers are immutable (not the

    case in every language) • Arrays, objects are not - so we need help here • JS and immutable data => opposing forces
  4. immutable.js • Great for simple collections • [1, 2, ...]

    ['a', 'b' ...] • Caveats for collections with objects https://github.com/facebook/immutable-js
  5. Immutable.js var record = {label: 'Sam'}; var list = Immutable.List([record]);

    ! console.log(list.get(0).label) // => 'Sam' http://jsbin.com/dilupu/1/embed?js,console record.label = 'Biff' ! console.log(list.get(0).label) // => 'Biff' Opps!
  6. immutable.js To be fair: Immutable.js can do the right thing

    if you learn it really well Immutable.js != Just works™
  7. var record = {label: 'Sam'}; var list = Immutable([record]); !

    console.log(list[0].label) // => 'Sam' ! record.label = 'Biff' ! console.log(list[0].label) // => 'Sam' http://jsbin.com/fayupu/1/edit?js,console Seamless immutable