有人可以解释以下代码吗?
(function() {
var name = "ritesh";
console.log("Hey " + name);
if (true) {
console.log("Hello " + name);
let name = "rohit";
}
})();
输出: 嘿ritesh VM126:7未捕获的ReferenceError:名称未定义
如果我注释一下初始化,相同的代码就可以正常工作
(function() {
var name = "ritesh";
console.log("Hey " + name);
if (true) {
console.log("Hello " + name);
//let name = "rohit";
}
})();
输出: 嘿ritesh 您好ritesh