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

  1. JavaScript ES6 Arrow Functions(箭头函数)

    1. 介绍 第一眼看到ES6新增加的 arrow function 时,感觉非常像 lambda 表达式. 那么arrow function是干什么的呢?可以看作为匿名函数的简写方式. 如: var ...

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

  3. Eloquent JavaScript #05# higher-order functions

    索引 Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop ...

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

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

  6. FusionCharts JavaScript API - Functions 常用方法整理笔记

    FusionCharts JavaScript API - Functions Home > FusionCharts XT and JavaScript > API Reference  ...

  7. 前端面试-----JavaScript题

    用面试题,复习一下,js基础. 1.综合题 function Foo() { getName = function () { alert (1); }; return this; } Foo.getN ...

  8. Duilib嵌入CEF以及JavaScript与C++交互

    转载:http://blog.csdn.net/foruok/article/details/50573612 转载:http://blog.csdn.net/foruok/article/detai ...

  9. 44个 Javascript 变态题解析 (上\下)

    第1题 ["1", "2", "3"].map(parseInt) 知识点: Array/map Number/parseInt JavaS ...

随机推荐

  1. javascript 获取用户光标,插入文本

    图1 如图1所示,点击[函数名称],将函数名称添加到表达式内容框中,点击参数名称,将参数名称index1作为方法的参数添加到表达式内容中的表达式中. 该功能实现主要是采用了javascript获取鼠标 ...

  2. (Error) The type AESKeyGenerator is not accessible due to restriction on required library.

    error for 'Access restriction: The type AESKeyGenerator is not accessible due to restriction on requ ...

  3. MySQL数据库中,使用游标循环遍历

    /*对*dt库下的所有数据表删除docuemttype为空和documenttype为MD,PD,ET的数据:delete from 表名 where length(documenttype)< ...

  4. JFrome 登陆/注册/回显/输出流小程序之二

  5. 『重构--改善既有代码的设计』读书笔记----Extract Method

    在编程中,比较忌讳的一件事情就是长函数.因为长函数代表了你这段代码不能很好的复用以及内部可能出现很多别的地方的重复代码,而且这段长函数内部的处理逻辑你也不能很好的看清楚.因此,今天重构第一个手法就是处 ...

  6. (转)Android Studio系列教程一下载与安装 背景Android Studio VS Eclipse准备下载创建HelloWorld项目

    背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,Goo ...

  7. js实现输入验证码

    html部分: <div> <input type="text" id="input" /> <input type=" ...

  8. jquery easy ui 学习 (1)Basic Window

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. C语言基础学习运算符-关系运算符

    比较大小 首先,我们得先了解一下布尔类型.C语言的C99标准支持布尔类型,关键字为_Bool,用于表示逻辑值true与false.C语言用值1表示true, 用值0表示false.因此布尔类型实际上是 ...

  10. TFS使用小技巧

    前述: TFS是微软自家的项目管理软件 link:TFS2012团队管理基本配置及基础使用方法 碰到的问题: 一.[高级—>获取特定版本] 今天做的一个项目,自己引用程序集出错,一直没找到解决方 ...