How does JS declare variables and functions without lexical scope in a statement block?

霸气de小男生 提交于 2019-12-05 00:22:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!