web-interview icon indicating copy to clipboard operation
web-interview copied to clipboard

[选择题] 60.(单选题)输出什么

Open qiilee opened this issue 5 years ago • 0 comments

const randomValue = 21;
function getInfo() {
  console.log(typeof randomValue);
  const randomValue = "Lydia Hallie";
}
getInfo();
A:"number"
B: "string"
C: undefined
D: ReferenceError

答案:D

解析:

通过 const 关键字声明的变量在被初始化之前不可被引用:这被称之为暂时性死区。在函数 getlnfo 中,变量 randomValue 声明在 getlnfo 的作用域的词法环境中。 在想要对 typeof randomValue 进行 log 之前,变量 randomValue 仍未被初始化:错误 ReferenceError 被抛出! JS 引擎并不会根据作用域链网上寻找该变量,因为我们已经在 getlnfo 函数中声明了randomValue 变量。

qiilee avatar Apr 15 '20 11:04 qiilee