Dealing with combining cases & duplicate cases in switch statements

前端 未结 4 603
猫巷女王i
猫巷女王i 2021-01-15 14:28

Is it okay to combine cases that share assignments and repeat the case for assignments that are not shared, or is it preferred to just keep each se

4条回答
  •  礼貌的吻别
    2021-01-15 14:39

    It can't work. As soon as a break instruction is executed, the remaining code in the switch block is not executed. You could fix it like this:

    switch(window.orientation) {
        case   0:
        case 180:
            w = 330;
        case   0:
            x = '-180px';
            break;
        case 180:
            x = '-80px';
            break;
        case -90:
        case  90:
            w = 480;
            x = '0';
            break;
        }
    

提交回复
热议问题