10_函数.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. var a = 10;
  72. }
  73. foo();
  74. console.log(a);
  75. function foo2(){
  76. var a = 10;
  77. }
  78. function foo3(){
  79. var a = 12;
  80. }
  81. var a = 13;
  82. var a = 14;
  83. </script>
  84. </body>
  85. </html>