1_let&const.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 = "str";
  12. // let 新的变量定义方式
  13. // let 定义的变量 不能重复定义
  14. // let a = 10;
  15. // let a = 1;
  16. // let b = "a";
  17. // a = 10;
  18. // var a ;
  19. // console.log(a);
  20. // let 定义的变量 不能在定义前使用 必须先定义再使用
  21. // a = 10;
  22. // let a;
  23. // console.log(a);
  24. // let 不存在变量提升
  25. // const 定义常量 不能重复定义 不能在定义前使用 必须先定义再使用
  26. // let a = 10;
  27. // a = 20;
  28. // a = "a";
  29. // console.log(a);
  30. // const b = 1;
  31. // b = 2;
  32. // console.log(b);
  33. var a = 10;
  34. let b = "hello";
  35. console.log(window.a);
  36. console.log(window.b);
  37. // var 存在变量提升
  38. // 变量提升:在代码执行前会先将 var 把变量定义的部分提升到当前作用域的最顶端
  39. // var a;
  40. // console.log(a);
  41. // a = 10;
  42. // function fun(){
  43. // var a
  44. // console.log(a);
  45. // a = 10;
  46. // }
  47. // console.log(a);
  48. // fun();
  49. // var foo;
  50. // console.log(foo);
  51. // foo = function(){
  52. // console.log("hello world");
  53. // }
  54. // foo()
  55. // var foo = function(){
  56. // console.log("hello world");
  57. // }
  58. // 函数提升:在代码执行前会先将函数定义的部分提升到当前作用域的最顶端
  59. // foo()
  60. // function foo(){
  61. // console.log("hello world");
  62. // }
  63. // foo();
  64. // function foo(){
  65. // console.log("hello world");
  66. // }
  67. // var foo = 10;
  68. // var foo;
  69. // function foo(){
  70. // console.log("hello world");
  71. // }
  72. // foo();
  73. // foo = 10;
  74. </script>
  75. </body>
  76. </html>