Skip to content

Instantly share code, notes, and snippets.

@Satyam
Created October 2, 2013 14:55
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 Satyam/6795060 to your computer and use it in GitHub Desktop.
Save Satyam/6795060 to your computer and use it in GitHub Desktop.
I tried a simple function to branch off to different functions depending on an argument, like a switch but indexed (and hopefully faster) and dynamic. I originally typed the first version in a single line in the debugging console. No need to tell me all the improvements it needs, this is not meant to be a production version. Here, I've just made…
// First version:
var goto = function (where) {
return {
a: function () {
console.log('a');
},
b: function () {
console.log('b');
}
}[where]();
};
goto ('b');
// Without the return doesn't work:
var goto = function (where) {
{
a: function () {
console.log('a');
},
b: function () {
console.log('b');
}
}[where]();
};
goto ('b');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment