请问如何简化这段 if else

if (type == 1) {
    if (state == 0) {
      this.aaa(activeId)
    } else if (state == 1) {
      this.qqq(type)
      this.clickNum = 1
    }
  } else if (type == 2) {
    if (state == 0) {
      this.bbb(hasFollow, shopId)
    } else if (state == 1 && hasFollow == true) {
      this.qqq(type)
      this.clickNum = 2
    }
  } else if (type == 3) {
    if (state == 0 || state == 1) {
      this.ccc()
    }
  } else if (type == 4) {
    if (state == 0) {
      this.ddd(skuId)
    } else if (state == 1) {
      this.qqq(type)
      this.clickNum = 4
    }
  } else if (type == 5) {
    if (state == 0) {
      this.$emit('shareId')
    }
  }
阅读 1.1k
评论
    4 个回答
    let a = 'this.qqq(type);this.clickNum = type'  
    switch (type) {  
      case 1:  
        (state == 0 && this.aaa(activeId)) || (state == 1 && eval(a))  
        break  
     case 2:  
        (state == 0 && this.bbb(hasFollow, shopId)) || (state == 1 && hasFollow == true && eval(a))  
        break  
     case 3:  
        (state == 0 || state == 1) && this.ccc()  
        break  
     case 4:  
        (state == 0 && this.ddd(skuId)) || (state == 1 && eval(a))  
        break  
     case 5:  
        state == 0 && this.emit('shareId')  
    }
      • 2
      • 新人请关照
      switch (type) {
        case 1:
          m(state, {
            '0': {
              callback: this.aaa,
              par: [activeId]
            },
            '1': {
              callback: n,
              par: [type]
            }
          })
          break;
        case 2:
          if (state == 0) {
            this.bbb(hasFollow, shopId);
            break;
          }
          if (state == 1 && hasFollow) {
            this.qqq(type)
            this.clickNum = type
            break;
          }
          break;
        case 4:
          m(state, {
            '0': {
              callback: this.ddd,
              par: [skuId]
            },
            '1': {
              callback: n,
              par: [type]
            }
          })
          break;
        case 3:
          m(state, {
            '0': {
              callback: this.ccc,
              par: []
            },
            '1': {
              callback: this.ccc,
              par: []
            }
          })
          break;
        case 5:
          m(state, {
            '0': {
              callback: this.$emit,
              par: ['shareId']
            }
          })
          break;
        default:
          break;
      }
      
      function m(state, callbackObj) {
        if (state in callbackObj) {
          let Obj = callbackObj[state]
          Obj.callback(...Obj.par)
        }
      }
      
      function n(type) {
        this.qqq(type);
        this.clickNum = type;
      }
        • 2.9k

        不需要优化,这是人最易读的代码。简化代码的后果就是,后期看起来费劲

          • 28

          代码是给人看的,不是给机器看的,不需要多短多精炼,首要准则是便于快速阅读。
          对你代码的唯一意见:else 看上去没有必要,直接用多个 if 即可。else if 相比于 if 更难阅读,因为阅读者需要去配合前面的 if 理解上下语境。

            撰写回答

            登录后参与交流、获取后续更新提醒

            相似问题
            推荐文章