The Immutable.js Record() allows you to model your immutable data much like you would model data with native Javascript classes or objects. It differs from native classes because it cannot be mutated after it's creation and it always has a default value. It's an excellent construct in which to piece together your stores, be them Flux or some other storage implementation. Let's quickly create an Immutable Record().

Create a Record class:

let TodoRecord = Immutable.Record({
id: (+new Date() + Math.floor(Math.random() * 999999)).toString(36),
title: "Default Title",
items: Immutable.List(),
completed: false
});

Inside the Recode class, you can define the default value. But you cannot add any method!

New a instance:

// Create a new instance
let imTodo = new TodoRecord({
title: "New Title",
items: Immutable.List(),
completed: false
});

Update and get an value:

// Update the title
imTodo = imTodo.updateIn(['title'], val => "Immutable");
console.log(imTodo.get('title'));

Read the value:

console.log(imTodo.title);

let items = imTodo.items;
let newItem = "New Item";
let updatedItems = imTodo.push(newItem); let newTodo = imTodo.set("items", updatedItems );

As you can see, the advantage by using Record instread of Map is that we can access the value directly by using:

imTodo.title;

instead of:

imTodo.get("title") //Of course this also works

最新文章

  1. VS Code
  2. AsyncTask异步任务类使用学习
  3. Python特殊语法:filter、map、reduce、lambda [转]
  4. linux centos安装编译phantomjs 2.0的方法
  5. 使用struts2+hibernate的增、删、改、查构架简单的学生管理系统
  6. 1088-Gnome Sequencing
  7. Java并发——使用Condition线程间通信
  8. 开源 免费 java CMS - FreeCMS2.0 会员我的评论
  9. Javascript实例技巧精选(6)—滚动鼠标中键读取Json数据分页显示网页内容
  10. Struts2学习第一天——struts2基本流程与配置
  11. 水题 第三站 HDU Largest prime factor
  12. javascript中的闭包(Closure)的学习
  13. docker(一) Centos7下安装docker
  14. Intellij Idea15 快捷键设置大全
  15. Xshell设置密钥登录CentOS6.5_64位(图文版)
  16. SQL Server全文搜索(转载)
  17. 函数和常用模块【day06】:模块特殊变量(十四)
  18. Git项目协同开发学习笔记2:项目库开发协作相关命令
  19. alter table导致的mysql事务回滚失败
  20. 如何使用Bootstrap4显示和隐藏元素

热门文章

  1. _js day9
  2. python - 类的方法
  3. C# 大小写转换
  4. css布局详解(一)——盒模型
  5. WndPric的使用方法
  6. asp net 编程问题 实现下一篇 和上一篇效果
  7. VC 串口通信类
  8. Go学习笔记(一):Ubuntu 环境下Go的安装
  9. Apache配置多个监听端口和不同的网站目录的简单方法(转)
  10. Introduction to the POM