【问题标题】:How hoisting works when function and variable have same name [duplicate]当函数和变量具有相同名称时提升如何工作[重复]
【发布时间】:2021-09-14 09:05:02
【问题描述】:

下面的 sn-ps - 函数的输出如何。

console.log(typeof foo);

function foo() {
  return "bar";
}
var foo = "bar";
function foo() {
  return "bar";
}
var foo;

console.log(typeof foo);

不应该因为 var foo 而未定义吗?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    var foo 冗余地声明了变量并且没有为它分配任何东西

    它没有显式分配undefined,因此它不会用undefined覆盖现有的函数值。

    【讨论】:

    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2015-09-19
    • 2020-10-26
    • 2016-06-28
    • 2011-09-15
    相关资源
    最近更新 更多