Friday, September 23, 2016

Functional Javascript - From Table to Objects

I modified the cvs example in Michael Fogus' book "Functional Javascript" to create an array of objects from a table. First example has a little hard-coding which is bad but that is eliminated in the following example. The third example uses _.object which is nice but a little magical for a beginner like me.



Without underscore:

Here we see that default values are declared for the arguments and a default return value! When no arguments are passed, the function returns a empty object.
function calcSalesTotal(total = 0, taxRate = 0.15 } = {}) {
    return total + (total * taxRate);
}