[Javascript] Refactoring: Polymorphic Functions
if-statements can add serious complexity and beg for refactoring. You can use polymorphic functions to simplify your ifs and dynamically call the appropriate method.
For example you have the code like:
function charge(){
if(this.moive.type==="regular"){
// ...some logic
if(this.daysRented > ){
// ...some logic
}
}else if(this.moive.type==="new release"){
// ...some logic
}else if(this.moive.type==="childrens"){
// ...some logic
if(this.daysRented > ){
// ...some logic
}
}
return amount;
}
We can refactor to:
require("activesupport")
this.charge = () => {
return this.[this.type.titleize().split(" ").join('').camelize() + "Charge"](daysRented);
}
this.regularCharge = ()=>{
if(daysRented > ){
}
}
this.newRelseaseCharge = () => {
}
this.childrenCharge = () => {
if(daysRented > ){
}
}
So based on the type we will call different function.
[Javascript] Refactoring: Polymorphic Functions的更多相关文章
- JavaScript ES6 Arrow Functions(箭头函数)
1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...
- [Javascript] Compose multiple functions for new behavior in JavaScript
In this lesson you will create a utility function that allows you to quickly compose behavior of mul ...
- Eloquent JavaScript #05# higher-order functions
索引 Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop ...
- [Javascript Crocks] Compose Functions for Reusability with the Maybe Type
We can dot-chain our way to great success with an instance of Maybe, but there are probably cases wh ...
- [Javascript] Run asynchronous functions in sequence using reduce
This can be handy if you have a rate limit on API requests or if you need to pass the result of each ...
- FusionCharts JavaScript API - Functions 常用方法整理笔记
FusionCharts JavaScript API - Functions Home > FusionCharts XT and JavaScript > API Reference ...
- 前端面试-----JavaScript题
用面试题,复习一下,js基础. 1.综合题 function Foo() { getName = function () { alert (1); }; return this; } Foo.getN ...
- Duilib嵌入CEF以及JavaScript与C++交互
转载:http://blog.csdn.net/foruok/article/details/50573612 转载:http://blog.csdn.net/foruok/article/detai ...
- 44个 Javascript 变态题解析 (上\下)
第1题 ["1", "2", "3"].map(parseInt) 知识点: Array/map Number/parseInt JavaS ...
随机推荐
- Protostuff自定义序列化(Delegate)解析
背景 在使用Protostuff进行序列化的时候,不幸地遇到了一个问题,就是Timestamp作为字段的时候,转换出现问题,通过Protostuff转换后的结果都是1970-01-01 08:00:0 ...
- JavaScript的“闭包”到底是什么(2)
我的上篇博客标题不对,造成一些误解.我认为博客的宗旨不是背教科书,而是分享研发心得.我的上篇标题因该改成“JavaScript 闭包的一个议题:它对outer scope 的影响”,因为我没有严格地去 ...
- ActiveReports 交互式报表之向下钻取解决方案
在 ActiveReports 中可以动态的显示或者隐藏某区域的数据,通过该功能用户可以根据需要显示或者隐藏所关心的数据,结合数据排序.过滤等功能可以让用户更方便地分析报表数据. 本文中展示的是销售数 ...
- MLlib 编程指导-spark-1.2.0
本文来自 http://spark.apache.org/docs/latest/mllib-guide.html 官方文档翻译 个人翻译 MLlib包括的算法和工具主要有:分类,回归,聚类,协同过滤 ...
- Java高精度学习第一弹
为了快速解决高精度问题,总算是要来接触java了,算上这学期要开java的课了,好好学习吧! 拿来练手的是hdu的1002,高精度加法. import java.util.*; import java ...
- ELK beats平台介绍
原文链接:http://www.tuicool.com/articles/mYjYRb6 beats是一个代理,将不同类型的数据发送到elasticsearch.beats可以直接将数据发送到elas ...
- Javascript 获取dom的宽度 随笔一
javascript 中 offsetWidth 获得的并不是 真实的宽度 不等于 style.width ;offetWidth 实际上获得的是物体的盒模型尺寸. 包括 border padding ...
- firefox插件poster的使用,发起自定义http请求
快捷键:ctrl+alt+p 在开发WEB程序的时候,经常需要模拟http请求,接收服务器响应,从而检验程序的正确性. Firefox插件poster,可以模拟各种http请求,并详细设置请求参数,比 ...
- org.quartz.utils.UpdateChecker Checking for available updated version of Quartz..
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</ ...
- POJ3267 The Cow Lexicon(dp)
题目链接. 分析: dp[i]表示母串从第i位起始的后缀所对应的最少去掉字母数. dp[i] = min(dp[i+res]+res-strlen(pa[j])); 其中res 为从第 i 位开始匹配 ...