带条件OR运算符的简写switch语句

奥德伦·特兰特(Aldren Terante)

有可能这样做吗?例如,“ a”或“ b”等于“ X”。如果'c'或'd'或'e'等于'Y'

var qwerty = function() {
    var month = 'a';
    var cases = {
      'a' || 'b' : month = 'X',
      'c' || 'd' || 'e' : month = 'Y'
    };
    if (cases[month]) {
      cases[month]();
    }
    return month;
};

console.log( qwerty() );

先感谢您 :)

KooiInc

不知道您的方法应该返回什么(现在它只返回'a')。这是可能的重写,以演示使用快捷方式布尔值评估的切换:

var qwerty = function(month) {
  return { month: /[ab]/.test(month) && 'X' || 
                  /[cde]/i.test(month) && 'Y' || 
                  'NOPES'};
};
qwerty('b').month; //=> 'X'
qwerty('x').month; //=> 'NOPES'

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章