Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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