[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 > 2){
            // ...some logic
        }
    }else if(this.moive.type==="new release"){
        // ...some logic
    }else if(this.moive.type==="childrens"){
        // ...some logic
        if(this.daysRented > 3){
            // ...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 > 2){

    }
} 

this.newRelseaseCharge = () => {

}

this.childrenCharge = () => {
    if(daysRented > 3){

    }
}

So based on the type we will call different function.

时间: 2024-10-12 18:58:58

[Javascript] Refactoring: Polymorphic Functions的相关文章

Javascript—Higher Order Functions

Higher order functions are functions that manipulate other functions. For example, a function can take other functions as arguments and/or produce a function as its return value. Such fancy functional techniques are powerful constructs available to y

Eloquent JavaScript #05# higher-order functions

索引: Notes 高阶函数 forEach filter map reduce some findIndex 重写课本示例代码 Excercises Flattening Your own loop Everything Dominant writing direction Notes 1.高阶函数的概念:Functions that operate on other functions, either by taking them as arguments or by returning t

[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 where you need to apply the same series of transformations against different Maybes. In this lesson, we’ll look at some point-free versions of some comm

[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 promise to the next one. function fetchMessages(username) { return fetch(`https://example.com/api/messages/${username}`) .then(response => response.j

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

第1题 ["1", "2", "3"].map(parseInt) 知识点: Array/map Number/parseInt JavaScript parseInt 首先, map接受两个参数, 一个回调函数 callback, 一个回调函数的this值 其中回调函数接受三个参数 currentValue, index, arrary; 而题目中, map只传入了回调函数--parseInt. 其次, parseInt 只接受两个两个参数 s

Javascript Query String Parsing

1. URL encoding 为什么要进行URL encoding?这是因为,有些字符是不能成为URL一部分的,举个例子,比如空格符.另外,有些有特殊含义的保留字符,比如#,作为HTML锚点,用于定位到HTML文档的某个位置上:=符号在URL里用于分割URL参数的key和value. URL encoding依据以下规则: 字母(A–Z 以及 a–z),数字(0-9)以及'.','-','~'和'_'这些字符不进行编码 空格符要编码成+或者"%20" 所有其他的字符,要被编码成%HH

44个 Javascript 变态题解析

原题来自: http://javascript-puzzlers.herokuapp.com/ 读者可以先去做一下感受感受. 当初笔者的成绩是 21/44... 当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了.... 不过, 对于基础知识的理解是深入编程的前提. 让我们一起来看看这些变态题到底变态不变态吧! 第1题 ["1", "2", "3"].map(parseInt) 知识点: Array/map Number/parseInt

[转] 有趣的JavaScript原生数组函数

在JavaScript中,可以通过两种方式创建数组,Array构造函数和 [] 便捷方式, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typeof操作符返回‘object’而不是‘array’.然而执 行[] instanceof Array返回true.此外,还有类数组对象使问题更复杂,如字符串对象,arguments对象.arguments对象不是Array的实例,但却 有个length属性,并且值能通过索引获取,所以能像数组一样通过循环操作. 在本文中,

有趣的JavaScript原生数组函数

在JavaScript中,创建数组可以使用Array构造函数,或者使用数组直接量[],后者是首选方法.Array对象继承自Object.prototype,对数组执行typeof操作符返回object而不是array.然而,[] instanceof Array也返回true.也就是说,类数组对象的实现更复杂,例如strings对象.arguments对象,arguments对象不是Array的实例,但有length属性,并能通过索引取值,所以能像数组一样进行循环操作. 在本文中,我将复习一些数