[Javascript] Manage Application State with Immutable.js

本文通过对比JavaScript原生数据类型和Immutable.js的数据结构,介绍了后者如何帮助开发者构建不可变的应用状态,从而简化状态管理并提高程序的可预测性和可靠性。

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.

转载于:https://www.cnblogs.com/Answer1215/p/4881252.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值