用hash表代替if

阅读更多
今天在重构时第一次处理短信用的if:
if(ApplyMessages.fore_two_string(json_message) == "BM"){
              ApplyMessages.process_apply_messages(json_message);
          }
if(ApplyMessages.fore_two_string(json_message) == "JJ" ){
             BidMessages.process_bid_messages(json_message);
          }

后来又换成了hash表形式:
var  fore_two_strings = ApplyMessages.fore_two_string(json_message);
        function judge_message() {
            var judge_message = {
                BM: function() {
                    ApplyMessages.process_apply_messages(json_message);
                },
                JJ: function() {
                    BidMessages.process_bid_messages(json_message);
                }
            }
            if(judge_message[fore_two_strings]) {//判断是否存在该键值
                judge_message[fore_two_strings]()//调用该键值对应的函数
            }
        }
        judge_message();

你可能感兴趣的:(hash代替if)