6_例题.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // var a = 99; //全局变量
  12. // f();
  13. // console.log(a); //输出全局的变量
  14. // function f() {
  15. // console.log(a); // undefined 因为var有变量提升
  16. // var a = 10; //在局部声明
  17. // console.log(a); // 10
  18. // }
  19. // 输出结果:
  20. /*
  21. undefined 10 99
  22. */
  23. // let a = 'aaa';
  24. // console.log(a);
  25. // console.log(window.a);//undefined
  26. // var b = 'bbb';
  27. // console.log(b);
  28. // console.log(window.b);
  29. // c = 'ccc';
  30. // console.log(c);
  31. // console.log(window.c);
  32. function a() {
  33. aa = 'aaaa';
  34. }
  35. a();
  36. console.log(aa);
  37. // function b() {
  38. // var bb = 'bbbb';
  39. // }
  40. // b();
  41. // // console.log(bb);
  42. // function c() {
  43. // let cc = 'cccc';
  44. // }
  45. // c();
  46. // console.log(cc);
  47. /* for 设置循环变量的部分 是一个父作用域 */
  48. /* 循环体内 是一个单独的子作用域 */
  49. // for (let i = 0; i < 3; i++) {
  50. // let i = 'aaa';
  51. // console.log(i);
  52. // }
  53. // for (var j = 0; j < 3; j++) {
  54. // var j = 'bbb';
  55. // console.log(j);
  56. // }
  57. /*
  58. x
  59. aaa bbb
  60. aaa aaa aaa bbb
  61. x bbb
  62. aaa bbb
  63. */
  64. for (let i = 0; i < 10; i++) {
  65. console.log(i);// 0 1 2 3 4 5 6 7 8 9
  66. setTimeout(function () {
  67. console.log(i);
  68. }, 0);
  69. }
  70. </script>
  71. </body>
  72. </html>