10_函数.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // var arr = [1,2,3,4,5];
  11. // var isInclude = false;
  12. // for(var i =0;i<arr.length;i++){
  13. // if(arr[i] == 3){
  14. // isInclude = true;
  15. // break;
  16. // }
  17. // }
  18. // console.log(isInclude)
  19. // console.log(arr.includes(3))
  20. // function 函数声明关键字
  21. // foo 代表函数名称
  22. // ()用作接受参数
  23. // {} 方法体
  24. // function foo(){
  25. // }
  26. // 函数定义及调用
  27. // function foo(){
  28. // console.log("hello");
  29. // }
  30. // foo();
  31. // 函数定义及传参
  32. // function foo(str){
  33. // console.log(str);
  34. // }
  35. // // foo("hello world");
  36. // var str1 = "hello world";
  37. // foo(str1);
  38. // 多参数函数
  39. // function addFun(a,b){
  40. // console.log(a+b);
  41. // }
  42. // addFun(1,2);
  43. // 带返回值的函数
  44. // function foo(){
  45. // // console.log("123");
  46. // return "loveCoding";
  47. // }
  48. // var str = foo();
  49. // console.log(str);
  50. // return 会结束函数的执行 后边的语句将不会执行
  51. // function foo(){
  52. // return "返回值";
  53. // console.log("hello");
  54. // }
  55. // foo();
  56. // 匿名函数定义方式
  57. // var foo = function(){
  58. // console.log("hello");
  59. // }
  60. // foo();
  61. // function foo(){
  62. // console.log("hello")
  63. // function foo2(){
  64. // console.log("world");
  65. // }
  66. // foo2();
  67. // }
  68. // foo();
  69. // 作用域
  70. function foo(){
  71. // 局部变量
  72. var a = 10;
  73. }
  74. foo();
  75. console.log(a);
  76. function foo2(){
  77. var a = 10;
  78. }
  79. function foo3(){
  80. var a = 12;
  81. }
  82. var a = 13;
  83. var a = 14;
  84. // 定义函数时参数为形参
  85. function foo(a){
  86. console.log(a)
  87. }
  88. //调用时的参数为实参
  89. foo(1)
  90. </script>
  91. </body>
  92. </html>