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. TimingTool - The Timing Diagram Editor

    TimingTool - The Timing Diagram TimingTool is designed to give electronics engineers an easy to use ...

  2. DWZ(JUI) 教程 跨域请求 iframeNavTab

    如果想navTab访问其他的网址,可以使用 iframe  navTab 使用时也非常简单 <li><a href="http://www.baidu.com"  ...

  3. Go 面试题(附答案解析)

    1.写出下面代码输出内容 package main import ( "fmt" ) func main() { defer_call() } func defer_call() ...

  4. epoll的LT和ET使用EPOLLONESHOT

    epoll有两种触发的方式即LT(水平触发)和ET(边缘触发)两种,在前者,只要存在着事件就会不断的触发,直到处理完成,而后者只触发一次相同事件或者说只在从非触发到触发两个状态转换的时候儿才触发. 这 ...

  5. Linux声卡驱动框图

    1.声卡驱动注册完成后的框图 2.open & hw_params 完.

  6. MySQL Workbench的安全更新模式

    MySQL Workbench上使用"DELETE FROM TABLE_E;"清空一个表时返回错误: Error Code: 1175. You are using safe u ...

  7. 查看webservice服务下的所有方法和参数类型

    方法:直接在IE浏览器中输入webservice的地址,查看返回的XML数据即可. 效果如下:

  8. C#编程(五十二)----------有序列表

    有序列表 如果需要基于对所有集合排序,就可以使用SortedList<TKey,TValue>类.这个类按照键给元素排序.这个集合中的值和键都可以使用任意类型. 下面的例子创建了一个有序列 ...

  9. Windows下安装WebLogic

    WebLogic安装结束 以下是进入MyEclipse启动配置WebLogic

  10. FEC详解三

    转自:http://blog.csdn.net/Stone_OverLooking/article/details/77752076 继续上文讲解: 3) 标准的RTP头结构如下所示: 其中第一个字节 ...