3
cb219
5y

In javascript, is there a difference between separate function calls that mimic a "chain pattern" or state changes using if/else if/else and using the chain or state pattern directly? The internet gave me no real/helpful response to that.

Suppose that:
if(isThingA(thing)) {
makeThingB();
else if(isThingB(thing)){
makeThingC();
else {
makeThingA();
}

That code is always executed e.g. after a user mouse click. "thing" gets defined in some other code.
It can be seen as a state machine that goes back to its starting point.
Is a pattern with objects/classes/prototypes even needed/preferred instead?
It's partly a problem I'm facing in my code but it's also interesting to know ideas/thoughts on this.

Comments
  • 0
    @M1sf3t to me, these 2 ways seem pretty much on par, like you can easily swap functions and conditions. With a pattern, there might be object creation(s) involved with equal interchangeability. So what now?🤷
    Also those "Best Practises" articles/videos are scattered everywhere, but I've never seen this particular kind of stuff covered, I guess they are just more general. More so all those patterns I've got to know from UML almost never work that way in JS, so you always have to rethink how to do it.🙄🙄
  • 0
  • 0
    Visitor pattern? A sequential list of states/steps?
Add Comment