1_新的声明方法.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 a = 10;
  11. // var a = "abc";
  12. // b = "123";
  13. // console.log(window.a);
  14. // window.document.getElementById
  15. // 不属于顶层对象window
  16. // let a = 10;
  17. // console.log(window.a);
  18. // let声明的变量,不允许重复声明
  19. // let a = 10;
  20. // let a = 20;
  21. // 变量提升 将变量定义部分提升到当前作用域的最顶端
  22. // var a;
  23. // console.log(a);
  24. // console.log(b);
  25. // var a = 10;
  26. // console.log(a);
  27. // var a
  28. // console.log(a);
  29. // a = 10;
  30. // 作用域 当前变量可作用的范围称之为作用域
  31. // var a
  32. // a = 100;
  33. // function foo(){
  34. // var a;
  35. // console.log(a);
  36. // a = 20;
  37. // }
  38. // foo();
  39. // 函数声明提升
  40. // function foo(){
  41. // console.log("helloworld");
  42. // }
  43. // foo();
  44. // console.log(foo)
  45. // 如果函数和变量同时出现提升那么以函数提升为主(变量的层级要高于函数的层级)
  46. // console.log(a);
  47. // function a(){
  48. // console.log("hello");
  49. // }
  50. // var a = 10;
  51. // console.log(a);
  52. // var a = 10;
  53. // var a = function(){
  54. // console.log(123);
  55. // }
  56. // let 不存在变量提升
  57. // console.log(a);
  58. // let a = 10;
  59. // let 暂时性死区 当前作用域下如果有使用let定义的变量那么在定义之前的位置不可以使用
  60. // function foo(){
  61. // console.log(a);
  62. // let a = 20;
  63. // }
  64. // foo();
  65. // if(true){
  66. // var a = 10;
  67. // }
  68. // function foo(){
  69. // var a = 20;
  70. // }
  71. // foo();
  72. // console.log(a);
  73. // 块级作用域使用let定义变量时 只要有大括号{} 就会产生一个作用域名
  74. if(true){
  75. let a = 10;
  76. }
  77. console.log(a);
  78. </script>
  79. </body>
  80. </html>