在if语句头声明的函数之后是未定义的

Function declared in the header of an if statement is undefined afterwards

本文关键字:函数 之后 未定义 声明 if 语句      更新时间:2023-09-26

为什么typeof f"undefined" ?不应该是"function"吗?

var x;
if (function f(){}) {
  x = typeof f;
}
console.log(x); // "undefined"

编辑:我为我的快速回答道歉,我没有读好这个问题。

但我知道这里发生了什么,因为函数的范围无法访问f的函数声明,typeof fundefinedx += typeof f将字符串"未定义"连接到x的值(这是1)。因此,您将1undefined作为x

的值