Created
October 2, 2013 14:55
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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