输出什么?
javascript
const randomValue = 21;
function getInfo() {
console.log(typeof randomValue);
const randomValue = "Lydia Hallie";
}
getInfo();A. "number" B. "string" C. undefined D. ReferenceError
答案: D
解析:
通过 const 关键字声明的变量在被初始化之前不可被引用:这被称之为 暂时性死区。在函数 getInfo 中, 变量 randomValue 声明在getInfo 的作用域的此法环境中。在想要对 typeof randomValue 进行log之前,变量 randomValue 仍未被初始化: 错误ReferenceError 被抛出! JS引擎并不会根据作用域链网上寻找该变量,因为我们已经在 getInfo 函数中声明了 randomValue 变量。