Alternative to the “switch” Statement

后端 未结 8 2075
眼角桃花
眼角桃花 2020-12-04 20:15

I do not want to use Switch in my code, so I\'m looking for some alternative

Example with Switch:

function write(what) {

  switch(w         


        
8条回答
  •  醉酒成梦
    2020-12-04 20:43

    An alternative is to define a class with a write method, and override that in subclasses Red and Blue to do the right thing.

    Whether or not that is better than your proposed solution, depends on your particular situation.

提交回复
热议问题