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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. JavaScript data types and data structures

    JavaScript data types and data structures Programming languages all have built-in data structures, b ...

  4. 如何选择Javascript模板引擎(javascript template engine)?

    译者 jjfat 日期:2012-9-17  来源: GBin1.com 随着前端开发的密集度越来越高,Ajax和JSON的使用越来越频繁,大家肯定免不了在前台开发中大量的使用标签,常见到的例子如下: ...

  5. 【JavaScript】javascript中伪协议(javascript:)使用探讨

    javascript:这个特殊的协议类型声明了URL的主体是任意的javascript代码,它由javascript的解释器运行. 比如下面这个死链接: <a href="javasc ...

  6. javascript实用技巧、javascript高级技巧

    字号+作者:H5之家 来源:H5之家 2016-10-31 11:00 我要评论( ) 三零网提供网络编程. JavaScript 的技术文章javascript实用技巧.javascript高级技巧 ...

  7. Javascript学习笔记3 Javascript与BOM简介

    什么是BOM BOM是browser object model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象 ...

  8. Javascript学习笔记1 javascript的特点

    ..对于网页而言,Javascript无处不在,对于英语不好的人它简直是噩梦般的存在,但形式所逼,今天开始着手学习!希望自己能坚持下去.从什么地方着手,我的目标是从大处着眼,从应用着眼,不抠细节,反正 ...

  9. 读Avoiding the Disk Bottleneck in the Data Domain Deduplication File System

    最近在思考和实践怎样应用重复数据删除技术到云存储服务中.找了些论文来读,其中<Avoiding the Disk Bottleneck in the Data Domain Deduplicat ...

随机推荐

  1. systemtap 脚本示例

    .[root@localhost ~]# stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}' Pass ...

  2. Python yield使用

    https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 您可能听说过,带有 yield 的函数在 Python 中被称 ...

  3. VS2008 LINK : fatal error LNK1104: cannot open file 'atls.lib'错误解决方案

    用VS 2008编写ATL的64位应用程序时,提示链接错误:VS2008 LINK : fatal error LNK1104: cannot open file 'atls.lib' 问题原因 VS ...

  4. NSString 和 NSData 转换

    NSString 转换成NSData 对象 NSData* xmlData =[@"testdata" dataUsingEncoding:NSUTF8StringEncoding ...

  5. 【linux】linux查看资源任务管理器,使用top命令 + 查看java进程下的线程数量【两种方式】

    ================================ 详解:https://blog.csdn.net/achenyuan/article/details/77867661 ======= ...

  6. 【docker】【Gitlab】gitlab中clone项目时,IP地址是一串数字(内网Gitlab的IP地址不正确)的问题解决

    首次在内网搭建Gitlab环境,在成功后在Gitlab上新建了一个项目. 然而在IDEA上clone项目时发现,项目地址如下: git@0096ce63c43f:root/jump.git 或者这样 ...

  7. codeforces Round #259(div2) C解题报告

    C. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  8. 安装veloeclipse插件报错解决方案

    步骤: 1.把Eclipse安装目录下的artifacts.xml打开,搜索veloeclipse,把它相关的项删除: 2.Help 3. Install New Software 4.Work Wi ...

  9. [Android Security] 如何把java代码转换成smali代码

    copy :https://www.cnblogs.com/gordon0918/p/5466514.html 1.概述 Smali是Android系统中Dalvik虚拟机指令语言,在apk逆向过程中 ...

  10. M2Crypto安装方法以及配置LDFLAGS、CFLAGS

    python3.7+mac环境: $ brew install openssl && brew install swig $ brew --prefix openssl /usr/lo ...