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. Linux安装配置mongodb

    1. 下载MongoDB 2. 解压文件到某目录下,然后重命名: [root@localhost var]# tar -xzvf mongodb-linux-i686-2.0.1.tar [root@ ...

  2. SQL利用临时表实现动态列、动态添加列

    --方法一--------------------------------------------------------------------- declare @sql as varchar(1 ...

  3. 使用Instant Client配置PL/SQL Developer

    之前使用PL/SQL Developer都是直接在本机安装完整版的Oracle Database,一是省事,二是可以在本机做一些demo测试:最近换了台电脑,感觉Instant Client更简单一些 ...

  4. centos5.2 x86 安装 oracle 11g2r 日志

    一.安装centos 二.安装ora所需的库 三.修改centos内核 四.建用户组和目录结构等 五.安装ora11g2r 六.安装sqlplus的翻页程序和help补丁 七.自启动脚本 八.常用命令 ...

  5. AFN发送请求失败

    发送请求出现这个错误 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Coc ...

  6. 11_RHEL安装Maya2015

    1. 解压 tar -xvf ./Autodesk_Maya_English_2015_Linux_64bit.tgz 2. 运行 ./setup 2.1补充 如果提示缺少 libpng12.so.0 ...

  7. JavaScript-学习一全局变量

    因为局部变量只作用于函数内,所以不同的函数可以使用相同名称的变量. 局部变量在函数开始执行时创建,函数执行完后局部变量会自动销 不限制位置的 JavaScript 变量生命周期在它声明时初始化. 局部 ...

  8. ActiveX控件资料

    Visual Studio 2008(c#)开发ActiveX控件及制作CAB包总结(1) 分类: C#2011-05-27 15:50 403人阅读 评论(0) 收藏 举报 c#stringhook ...

  9. eclipse中如何导入jar包

    如图,首先右键点击项目,选择最下面的properties, 然后进去之后点击java build path,右边会出来4个选项卡,选择libraries, 这时候最右边会有多个选项,第一个add ja ...

  10. php while循环 指定显示内容 例如不想显示前10条和后10条

    <?php //查询信息总的条数 $db_num = query_num("表","where 1=1"); //每页显示的条数 $page_size=2 ...