let,const,var关于暂时性死区

阅读 28

2022-01-31

暂时性死区:

  • 定义:let/const 命令会使区块形成封闭的作用域。若在声明之前使用变量,就会报错。总之,在代码块内,使用 let 命令声明变量之前,该变量都是不可用的。这在语法上,称为 “暂时性死区”
  • 现象:
    console.log(a) // ReferenceError: a is not defined
    let a

浅谈let,const,var关于暂时性死区:

  • var不存在暂时性死区,而let,const存在,所以let,const在变量声明之前就访问变量的话,会直接提示 ReferenceError,而不像var那样使用默认值 undefined
  • reason:

精彩评论(0)

0 0 举报