一、重复声明

  1. // var a = 12;
  2. // var a = 13;
  3. // console.log(a); // 13
  4. // let a = 12;
  5. // let a = 13; // Uncaught SyntaxError: Identifier 'a' has already been declared
  6. // console.log(a);
  7. /*
  8. * 全局作用域(栈内存)
  9. * 1.变量提升
  10. * 2.代码执行
  11. */
  12. // console.log(1); // 这行代码就已经不会执行了
  13. // let a = 12;
  14. // console.log(a);
  15. // let a = 13; // Uncaught SyntaxError: Identifier 'a' has already been declared
  16. // console.log(a);
  17. // console.log(1); // 1
  18. // console.log(a); // Uncaught ReferenceError: Cannot access 'a' before initialization
  19. // let a = 12;
  20. // 所谓重复是:不管之前通过什么办法,只要当前栈内存中存在了这个变量,我们使用let / const等重复再声明这个变量就是语法错误
  21. // console.log(a);
  22. // var a = 12;
  23. // let a = 13; // Uncaught SyntaxError: Identifier 'a' has already been declared
  24. // console.log(a);
  25. fn();
  26. function fn(){ console.log(1); }
  27. fn();
  28. function fn(){ console.log(2); }
  29. fn();
  30. var fn = function(){ console.log(3); }
  31. fn();
  32. function fn(){ console.log(4); }
  33. fn();
  34. function fn(){ console.log(5); }
  35. fn();

关于重复声明的问题.png

二、暂时性死区

  1. // console.log(a); // Uncaught ReferenceError: a is not defined
  2. // console.log(typeof a); // "undefined" 这是浏览器的BUG,本应该是报错的,因为没有a(暂时性死区)
  3. console.log(typeof a); // Uncaught ReferenceError: Cannot access 'a' before initialization
  4. let a;