先说结论
1.let,const 有变量提升 2.let,const的变量提升标注了暂时性死区的开始 3.你在暂时性死区里使用,js就会报错
根据
- 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.
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.
let的执行过程
- 先是声明变量 //暂时性死区开始
- 初始化 //赋值为undefined
- 赋值//暂时性死区结束
结论
1.我们应该关注的是暂时性死区,不要在暂时性死区的时候去使用。
2.提升不如说是暂时性死区的开始
3.从let出现那一刻开始,let暂时性死区开始,直到被初始化undefined或者赋值结束
4.其实每个人都有自己的理解,写好代码不出错是硬道理
参考资料
2.你不知道的js
3.你不知道的js