I have been thinking about the lack of the the switch statement in Python and wondered if the work around, using a dictionary, is more or less efficient in JavaScript?
So for example, I am building a Python speech recognition service that looks up a Python dictionary of functions to call:
services = {
"google": google_service(),
"netflix": netflix_service(),
"youtube": youtube_service(),
}
In JavaScript, I have traditionally seen switch statements or even if / else statements used for this purpose.
switch(service) {
case 'netflix':
return netflix_service();
}....
I am wondering wondering whether you can just use the object literal in JS, which to me at least seems more syntactically concise.
Can someone let me know best practice here.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire