函数的使用场景- 作为其他变量的值

函数作为其他变量的值。可以直接调用;

 1 <!DOCTYPE html>
 2 <html lang="zh">
 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     <script>
14 
15 
16 
17         function foo(x, y) {
18             if (x > y) {
19                 return x;
20             } else {
21                 return y;
22             }
23         }
24 
25         // 将 foo 函数作为变量 bar 的值
26         var bar = foo;
27 
28         console.log(foo(12, 88))
29         console.log(bar(12, 66))
30 
31     </script>
32 </body>
33 
34 </html>

猜你喜欢

转载自www.cnblogs.com/wszzj/p/12006805.html
今日推荐