12_函数.html 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // function foo(){
  11. // console.log("hello world");
  12. // }
  13. // foo();
  14. // function foo(str){//str 为函数参数 (形参)
  15. // console.log(str);
  16. // }
  17. // foo("hello world");//hello world 为函数实参 (实参)
  18. // 如果多个参数,用逗号隔开
  19. // function foo(a,b){
  20. // console.log(a+b);
  21. // }
  22. // foo(1,2)
  23. // 函数可以返回值 实用return 但如果执行renturn后,后面的代码将不再执行
  24. // function foo(a){
  25. // a = a * 1;
  26. // return a;
  27. // console.log("end");
  28. // }
  29. // var num = foo("123");
  30. // console.log(num + 1);
  31. // 匿名函数
  32. // var foo = function(){
  33. // console.log("hello world");
  34. // }
  35. // foo();
  36. // 函数内部可以访问全局变量
  37. var a = 20;
  38. function foo(){
  39. // 函数内部可以定义变量 但是不能被外部访问 局部变量
  40. // var a = 10;
  41. console.log(a);
  42. }
  43. foo();
  44. </script>
  45. </body>
  46. </html>