Personally, my favorite way to do JavaScript switches is to isolate them to their own function. If you're doing a simple dispatch, a function is already an appropriate abstraction, and return gives you the ability to avoid the break mess.
// Object literalfunctioncreateContent(contentType){constcontentTypes={post:Post,video:Video,default:Unknown};constcreateType=contentTypes[contentType]||contentTypes['default'];returncreateType();// or new createType() if its a class constructor}// SwitchfunctioncreateContent(contentType){switch(contentType){case"post":returnPost()// or new Post() if its a class constructorcase"video":returnVideo()default:returnUnknown()}}
Utilizing the switch statement also dodges the biggest drawback of the object literal: the location of the object. If you put the object literal in your dispatch function, you're creating the object every time you run the function. If you create the object once outside of the function, now your dispatch logic is in a different place than the actual dispatch call.
I wish JavaScript had a built in pattern matching construct, but a well used switch statement isn't all that bad.
Polyglot, autodidact. OSS author and contributor. Addicted to writing code, seeking my next 'fix'. Love communicating with an audience whose eyes don't glaze over when I get to the 'good parts'.
Love seeing these posts! I just recently used a switch in my code and didn't like the overall bulk the switch created. Nice ideas on how to clean it up!
// Object literalconstcreateContent=function(contentType){constcontentTypes=createContent.contentTypes;constcreateType=contentTypes[contentType]||contentTypes.default;returncreateType();// or new createType() if its a class constructor}createContent.contentTypes={post:Post,video:Video,default:Unknown};
Personally, my favorite way to do JavaScript switches is to isolate them to their own function. If you're doing a simple dispatch, a function is already an appropriate abstraction, and
return
gives you the ability to avoid thebreak
mess.Utilizing the switch statement also dodges the biggest drawback of the object literal: the location of the object. If you put the object literal in your dispatch function, you're creating the object every time you run the function. If you create the object once outside of the function, now your dispatch logic is in a different place than the actual dispatch call.
I wish JavaScript had a built in pattern matching construct, but a well used switch statement isn't all that bad.
Cool post ! :)
Why not wrap the whole thing in one function. Use it as a closure to store the type data then return the switch function to access it?
I like that
switch
versus the one I wrote. :DI recently discovered pattern matching via Scala and enjoyed using it!
Love seeing these posts! I just recently used a switch in my code and didn't like the overall bulk the switch created. Nice ideas on how to clean it up!
Example with no object creation in the function:
I love this solution, very simple;
I usually do this:
This is how redux architecture works ;)