请在下图中找到代码。1. 将实际抛出错误的函数的返回值赋给使用关键字'let'声明的变量'withLet'。2.调用'withLet',出现错误:'withLet is not defined'。3.尝试使用'let'断言'withLet',错误显示'withLet'已经被声明。
但是“var”不存在悖论(请在下图中找到)。
我很好奇是什么导致了这两种情况之间的不同行为。“未定义”和“已经声明”描述了相同的变量,这是非常连贯的。
let withLet = (function() {throw 'error!'})()
var withVar = (function() {throw 'error!'})()
//VM2470:1 Uncaught error!
//(anonymous) @ VM2470:1
//(anonymous) @ VM2470:1
withLet
//VM2484:1 Uncaught ReferenceError: withLet is not defined at
//<anonymous>:1:1
//(anonymous) @ VM2484:1
withVar
//undefined
let withLet = 'sth'
//VM2520:1 Uncaught SyntaxError: Identifier 'withLet' has already been
//declared
//at <anonymous>:1:1
//(anonymous) @ VM2520:1
withVar = 'sth'
//"sth"
截屏: