zoukankan      html  css  js  c++  java
  • js 回调函数小例子

    js 回调函数小例子

     1     <script>    
     2     //将函数作为另一个函数的参数
     3         function test1(){
     4             alert("我是test1");
     5         }
     6         function test2(f){
     7             f()
     8         }
     9         //test2(test1)
    10     //将函数作为参数
    11         function test(x,u){
    12             return x+u()
    13         }
    14         function fu(){
    15              return 100;
    16         }
    17         //alert(test(10,fu))
    18     //将有参函数作为参数
    19         function test2(x,u){
    20             return x+u(x)
    21         }
    22         function fu(x){
    23              return x*x
    24         }
    25         //alert(test2(10,fu))
    26     </script>
     1 <!DOCTYPE html>
     2 <html lang="zh-cn">
     3 <head>
     4     <meta charset="utf-8">
     5     <title>1-2 课堂演示</title>
     6     <link rel="stylesheet" type="text/css" href="style.css">
     7 </head>
     8 <body>
     9     <script>
    10         //将有参函数作为参数
    11         function test2(x,u){
    12             return x+u(x)
    13         }
    14         function fu(x){
    15              return x*x
    16         }
    17         //alert(test2(10,fu))
    18 
    19         //使用函数的返回值最为另一个函数的参数
    20         function test(x,u){
    21             return x+u
    22         }
    23         //alert(test(10,fu(10))) //注意这里和上一个的区别---这里fu(10) 引用的是函数的返回值
    24         //alert(fu(10)) //这里fu(10) 引用的是函数的返回值
    25         //alert(fu)     //这里的fu 引用的是函数本身
    26         //入门视频第七章的例子
    27         // for(i=1;i<50;i++){
    28         //     if(i%3==0){
    29         //         document.write(i+'<br>')  
    30         //     }
    31         // }
    32 
    33         function checkNum(start,end,check){
    34                 for(i=start;i<end;i++){
    35                 if(check(i)){
    36                     document.write(i+'<br>')  
    37                 }
    38             }
    39         }
    40         function check(x){
    41             if(x%3==0){
    42                 return true;
    43             }else{
    44                 return false;
    45             }
    46         }
    47         checkNum(1,100,check)
    48         
    49 
    50     </script>
    51 </body>
    52 </html>
  • 相关阅读:
    Mac 上所有的命令行相关问题的总结
    ThinkPHP中的统计查询方法
    织梦任意页面调用{dede:field.content/}的方法
    PHP isset()、empty()、is_null()的使用区别详解
    Dede更新提示DedeTag Engine Create File False的解决办法
    cookie 和session 的区别
    织梦后台如何设置手机站
    PHP时间戳和日期转换
    dede列表页调用
    dede图集内容页调用
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/8576013.html
Copyright © 2011-2022 走看看