8_函数.html 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. function num(x, y) {
  12. console.log(x + y)
  13. }
  14. num(10, 5)
  15. /* JavaScript 函数通过 function 关键词进行定义,其后是函数名和括号()。
  16. 函数名可包含字母、数字、下划线和美元符号(规则与变量名相同)。
  17. 圆括号可包括由逗号分隔的参数 */
  18. // function area(){
  19. // var a = window.prompt('请输入三角形的底边长度',"")
  20. // var b = window.prompt('请输入三角形的高度',"")
  21. // var c = a*b/2
  22. // alert('三角形的面试是'+ c)
  23. // }
  24. // area()
  25. // var d = 111 //全局变量
  26. // function num1(){
  27. // var b = 123 //局部变量
  28. // console.log(b)
  29. // }
  30. // num1()
  31. // console.log(b)
  32. //局部变量只能在局部访问 在全局访问不到
  33. //全局变量 能在 所有地方 访问
  34. var z = 111;
  35. fun()
  36. console.log(z)
  37. function fun(){
  38. console.log(z)
  39. z = 110
  40. console.log(z)
  41. }
  42. </script>
  43. </body>
  44. </html>