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>
我的旨在学过的东西不再忘记(主要使用艾宾浩斯遗忘曲线算法及其它智能学习复习算法)的偏公益性质的完全免费的编程视频学习网站:
【读书编程笔记】fanrenyi.com;有各种前端、后端、算法、大数据、人工智能等课程。
版权申明:欢迎转载,但请注明出处
一些博文中有一些参考内容因时间久远找不到来源了没有注明,如果侵权请联系我删除。
AI交流资料群:753014672