Skip to content

Instantly share code, notes, and snippets.

@twitchy
Created June 11, 2014 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twitchy/26f6415d90ae6ec19fe3 to your computer and use it in GitHub Desktop.
Save twitchy/26f6415d90ae6ec19fe3 to your computer and use it in GitHub Desktop.
Dispatch Tables
var thingsWeCanDo = {
doThisThing : function() { /* behavior */ },
doThatThing : function() { /* behavior */ },
doThisOtherThing : function() { /* behavior */ },
default : function() { /* behavior */ }
};
var doSomething = function(doWhat) {
var thingToDo = thingsWeCanDo.hasOwnProperty(doWhat) ? doWhat : "default"
thingsWeCanDo[thingToDo]();
}
var commandTable = {
north: function() { console.log("north"); },
east: function() { console.log("east"); },
south: function() { console.log("south"); },
west: function() { console.log("west"); },
}
function testDispatchTable(command) {
commandTable[command]();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment