码迷,mamicode.com
首页 > 移动开发 > 详细

[Javascript] Manage Application State with Immutable.js

时间:2015-10-15 06:23:18      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

Learn how Immutable.js data structures are different from native iterable Javascript data types and why they provide an excellent foundation on which to build your application‘s state.

 

Instead of them being mutable, they‘re always immutable, meaning they don‘t change from underneath you. The reference to them can change, but the data inside them cannot, which means you can build predictable and reliable state models on top of them. It becomes a lot easier to manage your application‘s state.

 

console.clear();

const ary = ["todo1", "todo2"];
const ary2 = ary;
console.log(ary[0]); // todo1

ary2[0] = "done1";
console.log(ary[0]); // done1

// Immutable 

function updateState(immutable, pos, value) {
  return immutable.set(pos, value);
}

const immutableState = Immutable.List(["foo1", "foo2"]);
const immutableState2 = immutableState.set(0, "bar1");

console.log(immutableState.get(0)); // foo1
console.log(immutableState2.get(0)); // bar1

 

Every time you use set() to set a new value, Immutable will return a new array.

[Javascript] Manage Application State with Immutable.js

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4881252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!