[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 ...
随机推荐
- List、Set、Map的使用
1.List(接口) List接口的特点: a.List接口可以存放任意数据,且在接口中,数据可以重复. b.List中提供了高效的插入和移除多个元素的方法. List常用的子类 a.ArrayLis ...
- C# Double String互转
/// <summary> /// str转金额 元 /// </summary> /// <param name="money"></p ...
- Java面试——基础
1,作用域,Java只有public,protect,private,默认是default相当于friendly 作用域 当前类 同一package 子类 其它 ...
- TCP连接的状态分析
1.先来了解一下TCP连接建立与关闭过程中的各种状态: CLOSED:初始状态,表示没有任何连接.LISTEN:Server端的某个Socket正在监听来自远方的TCP端口的连接请求.SYN_SENT ...
- 嵌入式开发(一) Ubuntu12.04下搭建交叉编译环境
操作系统:Ubuntu12.04 AMD64位 交叉编译环境:arm-Linux gcc版本4.4.3 前言: 首先理解一下交叉编译的意思.我们要给嵌入式设备写应用程序,但是又不能在嵌入式设备上完成所 ...
- RB1001: IE6 IE7 IE8(Q) 负边距 (margin) 导致元素溢出 hasLayout 容器时显示异常
标准参考 根据W3C CSS2.1规范第8.3节中的描述,边距属性设置了一个框的边距区的宽度.'margin' 缩写属性设置所有四边的边距,而其它的边距属性( 'margin-top' ,'margi ...
- Linux下的Job Control(转:http://blog.chinaunix.net/uid-26495963-id-3062757.html)
一.Job的概念 Job是指在批处理的环境中,为完成某一任务而进行一系列操作的总称.在个人接触计算机的年代,批处理的环境已经不容见到了,只有一些特殊的行业和环境下还在使用这样的概念,仅在书本中接触过. ...
- js移动设备手机跳转地址代码
if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alc ...
- jquery 节点操作大全
$para.attr("title"); 实例: <script type="text/javascript"> //<![CDATA[ $( ...
- 网站安全分析:恶意DOS脚本日志分析报告
http://www.chinaz.com/web/2012/0820/270205.shtml http://www.searchdatacenter.com.cn/showcontent_5817 ...