09-回调函数案例

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8 <script>
 9     //什么情况下,使用回调函数?
10     //回调函数一般是用于定义一个规则来使用的。
11     //规则的传递只能通过函数实现。通过变量无法达成。所以我们需要传递规则的时候必须使用回调函数。
12     console.log(fn(10,5,test1));
13     console.log(fn(10,5,test2));
14     console.log(fn(10,5,test3));
15     console.log(fn(10,5,test4));
16 
17 
18     function fn(num1,num2,demo){
19         return demo(num1,num2);
20     }
21 
22     //定义四个规则:加减乘除
23     function test1(a,b){
24         return a+b;
25     }
26     function test2(a,b){
27         return a-b;
28     }
29     function test3(a,b){
30         return a*b;
31     }
32     function test4(a,b){
33         return a/b;
34     }
35 
36 
37 </script>
38 </body>
39 </html>
原文地址:https://www.cnblogs.com/BingBing-Deng/p/10266999.html