Skip to content

Instantly share code, notes, and snippets.

@tangrammer
Last active December 10, 2015 02:09
Show Gist options
  • Save tangrammer/4365658 to your computer and use it in GitHub Desktop.
Save tangrammer/4365658 to your computer and use it in GitHub Desktop.
dispatch table pattern to obtain the localized date printer for one person Note that the returned function expect the date in its closure scope "this.date" It use date_format.js lib to augmenting Date function with format method. Obtained from http://jsfiddle.net/phZr7/1/
/* functions printers outside for better reuse*/
var date_printer={
//US = United States, UK = United Kingdom, AU = Australia
US: function(){return "US: "+this.date.format("mm/dd/yy");},
UK: function(){return "UK: "+this.date.format("dd/mm/yy");},
AU: function(){return "AU: "+this.date.format("dd/mm/yy");}
};
var context={date:"1976-06-13"};
var us_printer=date_printer.US;
us_printer.call(context); // return US: 06/13/76
var au_printer=date_printer.AU;
au_printer.call(context); // return AU: 13/06/76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment