js条件语句之职责链数组

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5     <meta charset="UTF-8">
 6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8     <title>Document</title>
 9 </head>
10 
11 <body>
12 
13 </body>
14 <script>
15     const rules = [{
16         match: function(a, b, c) {
17             console.log(555);
18         },
19         action: function(a, b, c) {
20             console.log(555);
21         }
22     }, {
23         match: function(a, b, c) {
24             console.log(666);
25         },
26         action: function(a, b, c) {
27             console.log(666);
28         }
29     }, {
30         match: function(a, b, c) {
31             console.log(777);
32         },
33         action: function(a, b, c) {
34             console.log(777);
35         }
36     }]
37 
38     function demo(a, b, c) {
39         for (let i = 0; i < rules.length; i++) {
40             if (rules[i].match(a, b, c)) {
41                 return rules[i].action(a, b, c)
42             }
43         }
44     }
45 
46     demo(a, b, d);
47 </script>
48 
49 </html>

 

posted @ 2017-10-30 16:13  Sunsin  阅读(256)  评论(0编辑  收藏  举报