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)

猜你喜欢

转载自www.cnblogs.com/love314159/p/9046161.html