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