Skip to content

Instantly share code, notes, and snippets.

@pragmaticobjects
Created September 11, 2011 04:37
Show Gist options
  • Select an option

  • Save pragmaticobjects/1209177 to your computer and use it in GitHub Desktop.

Select an option

Save pragmaticobjects/1209177 to your computer and use it in GitHub Desktop.
Closure to replace switch case
var calculateTotal = function f(item) {
var tax= 0.08;
var fee = 10;
var sinTax = 0.02;
var h = {
'gas': function(price) {
return price + price*tax + fee;
},
'wine': function(price) {
return price + price*tax + price*sinTax;
}
};
return (typeof h[item] == 'undefined') ?
(function(price) {
return price + price*tax;
}): h[item];
};
var total = calculateTotal('milk')(100);
alert(total);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment