Immutable.js 笔记
约定变量命名规则:如所有Immutable类型对象以`$`开头。
foo = Immutable.fromJS({a: {b: 1}});
bar = foo.setIn(['a', 'b'], 2);
var nested3 = nested2.updateIn(['a', 'b', 'd'], value => value + 1);
data:Map({ times:0 })
this.setState({ data:this.state.data.update('times', v=>v+1) });
等价于
this.setState(({data})=>({
data:data.update('times', v=>v+1) })
});
React.PropTypes.instanceOf(Immutable.List).isRequired
List
List.isList()
下标-1,表示数组最后一个元素
immutableData.sort (a, b) ->
if a < b then return -1
if a > b then return 1
return 0
immutableData.sortBy (x) ->
x
immutableData.find (x) ->
x > 1
immutableData.filter (x) ->
x > 1
immutableData.filterNot (x) ->
x <= 1
var list1 = Immutable.List.of(1, 2);
var list2 = list1.push(3, 4, 5);
var list3 = list2.unshift(0);
var list4 = list1.concat(list2, list3);
list.splice(index, 1)
list.insert(index,value)
list.clear()
unshift()在数组头添加数据,返回新数组
shift()删除首项,返回新数组
var list1 = Immutable.List.of(1,2,3);
var list2 = list1.withMutations(function (list) {
list.push(4).push(5).push(6);
});