if-else(职责链)

var a=1,b=2,c=3,d=4;
        
        
        const rules = [
          {
            match: function (a, b, c,d) { return a;},
            action: function (a, b, c,d) { console.log(a) }
          },
          {
            match: function ( a, b, c,d) { return b; },
            action: function (a, b, c,d) { console.log(b) }
          },
          {
            match: function (a, b, c,d) { return c; },
            action: function (a, b, c,d) { console.log(c) }
          },
          {
            match: function (a, b, c,d) { return d;},
            action: function (a, b, c,d) {console.log(d) }
          }
        ]
        function demo (a, b, c,d) {
          for (let i = 0; i < rules.length; i++) {
            if (rules[i].match(a, b, c,d)==2) {
              return rules[i].action(a, b, c)
            }
          }
        }
        demo (a, b, c,d)

 

posted @ 2018-05-16 15:15  风吹麦浪打  阅读(139)  评论(0编辑  收藏  举报