【问题标题】:Alternative to the "switch" Statement“switch”语句的替代方案
【发布时间】:2011-01-19 09:55:09
【问题描述】:

我不想在我的代码中使用 Switch,所以我正在寻找一些替代方法

开关示例:

function write(what) {

  switch(what) {

    case 'Blue':
      alert ('Blue');
    break;

    ...

    case 'Red':
      alert ('Red');
    break;

  }

}

不带开关的示例:

colors = [];

colors['Blue'] = function() { alert('Blue'); };
colors['Red'] = function() { alert('Red'); };


function write(what) {

  colors[what]();

}

我的问题是:

  1. 您知道其他替代方案吗?
  2. 这是最好的解决方案吗?

【问题讨论】:

  • 太棒了。离开吧。
  • 是的,这是更好的解决方案,代码更少:)
  • 它也更干净,更易于阅读,并且可能可以删除重复代码。

标签: javascript switch-statement


【解决方案1】:

另一种方法是使用write 方法定义一个类,并在子类RedBlue 中覆盖该方法以做正确的事情。

这是否比您提出的解决方案更好,取决于您的具体情况。

【讨论】:

    【解决方案2】:

    你已经差不多了。如果可能,您可能希望添加一个辅助函数以使设置更容易。例如:

    function setup(what)
    {
        colors[what] = function() { alert(what); };
    }
    

    编辑:
    如果您想为每个选项做的事情显然更复杂,这将不起作用。正如@roe 在 cmets 中所提到的,这使用了通常不受欢迎的全局颜色。

    【讨论】:

    • 这只适用于这个特定的例子,很可能它们在现实中没有那么多共同点。颜色在这里也是全局的,这很少是好事。
    【解决方案3】:

    问题 2:

    一般来说,如果您可以用字典查找替换自定义控制结构,那您就很好了。它易于阅读且非常优雅——坚持下去。

    【讨论】:

      【解决方案4】:

      正如我所说,这很棒。我唯一可以添加到您的解决方案中的是,最好将您的 colors 本地化。

      function write(what) {
          var colors = [];
          colors['Blue'] = function() { alert('Blue'); };
          colors['Red'] = function() { alert('Red'); };
          colors[what]();
      }
      

      【讨论】:

        【解决方案5】:

        我只有一个关于您的第二种方法的注释,您 shouldn't use an Array 来存储非数字索引(在其他语言中您可以将其称为 关联数组)。

        你应该使用一个简单的对象。

        另外,您可能想检查传递给您的 write 函数的 what 参数是否作为您的 colors 对象的属性存在并查看它是否是一个函数,因此您可以在没有运行时调用它错误:

        var colors = {};
        
        colors['Blue'] = function() { alert('Blue'); };
        colors['Red'] = function() { alert('Red'); };
        
        
        function write(what) {
          if (typeof colors[what] == 'function') {
            colors[what]();
            return;
          }
          // not a function, default case
          // ...
        }
        

        【讨论】:

        • 恕我直言,我认为您链接的文章有误。相反,不应该使用 for..in 来迭代数组。
        • @machine:那篇文章还谈到了for...in 语句以及当像ArrayObject 这样的内置构造函数的原型成员被扩展时出现的问题,但底线这篇文章的重点是 JavaScript 数组应该是数字的,它们通常用于存储任意键/值对,这是不好的做法...
        • 底线?在数组上使用 expando 属性并不会改变这一事实。将您可以使用的最通用的对象之一变成最通用的对象,并没有什么好处。我不知道你为什么说“这篇文章也会说话”......我的理解是,这篇文章的整个重点是“更好地”支持使用 for..in 和数组。
        【解决方案6】:

        您可以使用对象字面量,并尝试使用 catch 捕获默认值:

        function write(what) {
            var colors = {
            'Blue': function(){ alert('Light-Blue'); },
            'Red': function(){ alert('Deep-Red'); },
            'Green': function(){ alert('Deep-Green'); }
            }
            try {colors[what]();}
            catch(err) {colors['Green']();}//default behaviour
        }
        write('Pink');
        

        【讨论】:

          【解决方案7】:

          我今天使用了这样的结构:

          var chosenColor = 'red';
          
          var colorString = {
              'red': 'The color is red.',
              'green': 'The color is green.',
              'blue': 'The color is blue.',
          }[chosenColor] || 'The color is unknown.';
          

          我喜欢根据选择来选择字符串的代码量非常少。

          你也可以将它传递给一个函数:

          alert({
              'red': 'The color is red.',
              'green': 'The color is green.',
              'blue': 'The color is blue.',
          }[chosenColor] || 'The color is unknown.');
          

          【讨论】:

          • 短小精悍,喜欢。 +1
          • 这很酷,只要 selectedColor 存在于 colorString 中。例如,如果 selectedColor 为“橙色”,您将收到类型错误。
          • 事实上,您可以使用({'red': 'the color is red'})[chosenColor] || 'default'轻松添加默认选项
          • 感谢@superzamp,我已经编辑了我的代码以添加默认选项。
          • 在 2021 年使用 ?? 代替 ||
          【解决方案8】:

          我必须对列表的一组对象道具进行比较,并且不想为所有可能性做一个开关/案例,所以我首先将一个对象数组分配给一个数字等级,所以案例变成了一个简单的比较。这只是 4 种可能性,但您会知道如何将其扩展到开关/案例变得无法管理的情况:

          函数 mySort2(item1,item2){

               var matrix = {  
              'repair': 4,  
              'r/r': 3,  
              'part': 2,  
              'misc': 1  
            };  
          
          (matrix[item1.category] < matrix[item2.category]) ? return +1 : return -1;
          

          //如果可能有坏数据需要先检查这个???

          i1=matrix[item1.category] || null;
          i2=matrix[item2.category] || null;
          
          if (i1==null){
              // handle bad data in item 1
              return +1; // put it after 2
          }
          
          if (i2==null){
              // ditto 
              return -1; //put 1 first
          }
          
          if (i1<i2) 
              return +1;
          else 
              return -1;
          

          }

          【讨论】:

          • 这是一个答案:为此目的使用开关/外壳的替代方法。您还可以使用 [] 的 [] 来返回要返回的实际值矩阵。
          【解决方案9】:

          或者,你也可以使用字典,这样你就可以看到函数返回的类型,我认为它是干净和可扩展的,虽然它只是纯 JS。

          const ColorDictionary = {
            red: 'applies red color',
            blue: ' applies blue color',
            green: 'applies green color',
          }
          
          const useShowColors = (color) => {
          
           // color will be selected or fallout to default value.
           const getColor = () => (
            ColorDicionary[color] ?? 'applies default color' 
           )
           
           return { getColor }
          }
          
          const { getColor } = useShowColors() //pass the color you wish.
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-11-22
            • 2014-05-22
            • 1970-01-01
            • 2013-04-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多