[Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
To demonstrate the difference between mutability and immutability, imagine taking a drink from a glass of water. If our glass is mutable, when we take a drink, we retain the same glass and change the amount of water in that glass. However, if our glass is immutable, when we take a drink, we get back a brand new, identical glass containing the correctly drank amount. Perhaps a strange way to conceive of the action, but creating new data structures makes our methods pure and thread-safe, a benefit of functional programming.
class MutableGlass {
constructor(content, amount) {
this.content = content
this.amount = amount
} takeDrink(value) {
this.amount = Math.max(this.amount - value, )
return this
}
} // We can verify this by checking the references of the first glass and
// the glass returned by `takeDrink()` and see that they are the same.
const mg1 = new MutableGlass('water', )
const mg2 = mg1.takeDrink()
console.log(mg1.amount === && mg1.amount === mg2.amount) // true
console.log(mg1 === mg2) // true
Immutable class, whch every time should return a new instance:
// Taking a drink from the immutable glass returns an entirely new glass,
// but with the correct content and amount of it in the glass.
class ImmutableGlass {
constructor(content, amount) {
this.content = content
this.amount = amount
} takeDrink(value) {
return new ImmutableGlass(this.content, Math.max(this.amount - value, ))
}
} // We can verify this by checking the references and seeing that they are
// _not_ equal
const ig1 = new ImmutableGlass('water', )
const ig2 = ig1.takeDrink()
console.log(ig1.amount !== ig2.amount) // true
console.log(ig1 === ig2) // false
[Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures的更多相关文章
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
- [Javascript] Simplify Creating Immutable Data Trees With Immer
Immer is a tiny library that makes it possible to work with immutable data in JavaScript in a much m ...
- JavaScript data types and data structures
JavaScript data types and data structures Programming languages all have built-in data structures, b ...
- 如何选择Javascript模板引擎(javascript template engine)?
译者 jjfat 日期:2012-9-17 来源: GBin1.com 随着前端开发的密集度越来越高,Ajax和JSON的使用越来越频繁,大家肯定免不了在前台开发中大量的使用标签,常见到的例子如下: ...
- 【JavaScript】javascript中伪协议(javascript:)使用探讨
javascript:这个特殊的协议类型声明了URL的主体是任意的javascript代码,它由javascript的解释器运行. 比如下面这个死链接: <a href="javasc ...
- javascript实用技巧、javascript高级技巧
字号+作者:H5之家 来源:H5之家 2016-10-31 11:00 我要评论( ) 三零网提供网络编程. JavaScript 的技术文章javascript实用技巧.javascript高级技巧 ...
- Javascript学习笔记3 Javascript与BOM简介
什么是BOM BOM是browser object model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象 ...
- Javascript学习笔记1 javascript的特点
..对于网页而言,Javascript无处不在,对于英语不好的人它简直是噩梦般的存在,但形式所逼,今天开始着手学习!希望自己能坚持下去.从什么地方着手,我的目标是从大处着眼,从应用着眼,不抠细节,反正 ...
- 读Avoiding the Disk Bottleneck in the Data Domain Deduplication File System
最近在思考和实践怎样应用重复数据删除技术到云存储服务中.找了些论文来读,其中<Avoiding the Disk Bottleneck in the Data Domain Deduplicat ...
随机推荐
- JTAG – A technical overview and Timing
This document provides you with interesting background information about the technology that underpi ...
- IBDAP-CMSIS-DAP
IBDAP-CMSIS-DAP Armstart's CMSIS-DAP firmware implementation in gcc and makefile. http://www.armstar ...
- the difference between an embOS interrupt and a zero latency interrupt
the difference between an embOS interrupt and a zero latency interrupt is the interrupt priority lev ...
- lodoop打印控件详解
注意:使用此打印控件需要引入(在我上传的Demo中都有): install_lodop32.exe install_lodop64.exe LodopFuncs.js jquery-1.10.0.mi ...
- 百度公共dns
常用公共DNS服务器地址 DNS(Domain Name System),即域名解析系统,作为将域名和IP地址相互映射的一个分布式数据库,能够使人通过相对好记的域名访问网站,而是一串数字.目前国内运营 ...
- [Winform]Media Player播放控制面板控制,单击事件截获
摘要 在项目中有这样的一个需求,需要在一台宣传机器上循环播放一段视频,并在体验的用户单击鼠标左键的时候推出全屏,可以让用户体验电脑的其它功能. 解决方案 考虑到都是windows系统的,所以采用了wi ...
- Linux init 0-6 启动级别
原文地址:http://blog.sina.com.cn/s/blog_5f8e8d9801010wlr.html 原文地址:[转]Linux init 0-6 启动级别作者:流水清风 init 0- ...
- iOS-Xcode必备插件XAlign:瞬间优化你的代码
今天向大家介绍一个非常好用的Xcode代码编辑插件,这个插件可以很快速地使代码对齐,有3种模式:“=”对齐.宏定义对齐和属性对齐 XAlign效果图 1.“=”对齐 2.宏定义对齐 3.属 ...
- log4j1修改DailyRollingFileAppender支持日志最大数量
配置说明: log4j.appender.logfile=org.apache.log4j.MyDailyRollingFileAppender log4j.appender.logfile.File ...
- nginx反向代理vue访问时浏览器加载失败,出现 ERR_CONTENT_LENGTH_MISMATCH 问题
问题说明:测试机上部署了一套业务环境,nginx反向代理tomcat,在访问时长时间处于加载中,十分缓慢! 通过浏览器调试(F12键->Console),发现有错误ERR_CONTENT_LEN ...