let到底有没有变量提升

先说结论

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

根据

  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.

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的执行过程

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

结论

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

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

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

4.其实每个人都有自己的理解,写好代码不出错是硬道理

参考资料

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

2.你不知道的js

3.你不知道的js

4.stack-overflow上的高赞回答

转载于:https://juejin.im/post/5aab3334518825558358fc7c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值