[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 ...
随机推荐
- excel - 统计字符个数综合案例
本文通过一个综合的案例来介绍excel统计字符数的一些方法和思路,供大家参考和学习. 下图是一个excel数据源截图,我们逐一讲解不同条件的统计字符数. 第一,统计A2所有的字符数,不论是汉字和数字. ...
- JQuery验证input
jsp中表单如下: <form method="post" action="AddPlayer"> <div class = "on ...
- php cookie不刷新及时生效的实现代码
<?php /** * 不刷新 cookie及时生效 */ cookie("mycookie","cookievalue",time()+60); coo ...
- 浅谈MIPS地址对齐问题
1.什么叫地址对齐? RISC 下使用访存指令读取或写入数据单元时,目标地址必须是所访问之数据单元字节数的整数倍,这个叫做地址对齐. 2.计算机主要的架构分哪两类?及其地址对齐在两者的区别? 计算机主 ...
- jquery获取浏览器的高度和宽度
<script type="text/javascript"> $(document).ready(function() { alert($(window).heigh ...
- spring的beans.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- CSU 1160(进制问题)
CSU 1160 Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Descrip ...
- [转载]如何查看某个查询使用了多少TempDB空间
http://www.cnblogs.com/CareySon/p/3910337.html 通过下面脚本可以查看某个查询实用的TempDB的空间. 第一步是查询出当下TempDB实用空间是多少, 第 ...
- opencv 用户文档 错误更正 仿射变换
今天在看opencv官方给出的仿射变换计算仿射变换矩阵的文档的时候,发现官方文档中有个很明显的错误,再次给大家提个醒. 官方文档连接: http://opencv.willowgarage.com/d ...
- CSDN总结的面试中的十大可视化工具
1. D3.js 基于JavaScript的数据可视化库,允许绑定任意数据到DOM,然后将数据驱动转换应用到Document中. 2. Data.js Data.js是一个JavaScript数据表示 ...