1

The scene is like the following code, but the reality does not feel very good.

//example
const data = [];
const index = 0;

if (index === 0) {
  A(data);
} else if (index === 1) {
  B(data);
} else if (index === 2) {
  C(data);
}
  • 1
    Chain of Responsibility comes to the rescue. Visit https://stackoverflow.com/questions/53684822/is-the-chain-of-responsibility-pattern-a-good-replacement-for-a-sequence-of-cond – Mr. Brickowski Aug 11 '22 at 08:32

1 Answers1

0

First: You could declare an object that has all of your functions

let functions = {
   0: function(data) { A(data) },
   1: function(data) { B(data) },
   2: function(data) { C(data) }
}

And then you can use it like this:

functions[index](data);

I'm not saying it's better than what you have currently but its different. Maybe it helps you.