console.log(window.b,b) //undefined undefined
if(true){
console.log(b) //ƒ b(){} does it get hoisted?
b = 4
function b(){}
b = 3
console.log(b) //3 Why is this variable 3
}
console.log(window.b,b) //4 4 Why is this variable 4
如果代码删除此行
function b(){}浏览器将在第1行console.log(window.b,b)处报告错误VM114:1 Uncaught ReferenceError: b is not defined
谁能告诉我为什么它是这样工作的?
如果function b被提升,它是否等于
console.log(window.b,b) //undefined undefined why it doesn't cause an ReferenceError b is not defined
if(true){
function b(){}
b = 4
b = 3
console.log(b) // 3
}
console.log(window.b,b) // ƒ b(){} ƒ b(){}
我不是以英语为母语的人,希望你能理解。
发布于 2020-08-14 22:00:33
因为您的函数b被提升了。一旦它到达b = 4,就会给它赋值。
https://stackoverflow.com/questions/63414209
复制相似问题