let到底有没有变量提高

先说结论

1.let,const 有变量提高 2.let,const的变量提高标注了暂时性死区的开始 3.你在暂时性死区里使用,js就会报错git

根据

  1. ECMA-262.pdf

13.3.1 Let and Const Declarations let and const declarations define variables that are scoped to the running execution context's LexicalEnvironment. The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable's LexicalBinding is evaluated.es6

18.2.1.2 Runtime Semantics: EvalDeclarationInstantiation( body, varEnv, lexEnv, strict) The environment of with statements cannot contain any lexical declaration so it doesn't need to be checked for var/let hoisting conflicts.github

let的执行过程

  1. 先是声明变量 //暂时性死区开始
  2. 初始化 //赋值为undefined
  3. 赋值//暂时性死区结束

结论

1.咱们应该关注的是暂时性死区,不要在暂时性死区的时候去使用。lua

2.提高不如说是暂时性死区的开始get

3.从let出现那一刻开始,let暂时性死区开始,直到被初始化undefined或者赋值结束it

4.其实每一个人都有本身的理解,写好代码不出错是硬道理io

参考资料

1.知乎专栏上的一篇解释ast

2.你不知道的jsclass

3.你不知道的jspdf

4.stack-overflow上的高赞回答

相关文章
相关标签/搜索