问题
var a;
{
function a() {}
a = 60;
console.log('1: ', a);
}
console.log('2: ', a);
var b;
{
b = 60;
function b() {}
console.log('3: ', b);
}
console.log('4: ', b);
The output is:
1: 60
2: f a() {}
3: 60
4: 60
I can't figure out why, if I remove the curly braces, it all prints 60
. Perhaps because of hoisting. But a function declaration doesn't have lexical scope as far as I know, and even if it does, the first output should print the function, right?
来源:https://stackoverflow.com/questions/58151953/how-does-js-declare-variables-and-functions-without-lexical-scope-in-a-statement