为什么 typeof 是 "inline" 函数 "function" 而 non-inline 是另一种变量类型?

Why is typeof an "inline" function "function" and non-inline another variable type?

自从我在函数调用中了解到 callbacks/functions 后,我认为它们与具有名称的函数相同。

但在我测试时,看起来如果两个函数 return 一个字符串,内联回调样式一个 returns typeof 函数,但实际函数调用 return s 字符串类型。让我证明我的意思:

function t(s) {
  return typeof s;
}
console.log("Inline function type: " + t(function() {
  return typeof "Hello world!";
}));
console.log("Non-inline with function name: " + t(t()));

为什么会这样?

解压缩 t(t())) 你会得到:

t(t()))
t(t(undefined)))
// type of undefined is the string 'undefined', so you get
t('undefined'))
// type of 'undefined' is a string, so
'string'

另一种方法:

t(function() {
  return typeof "Hello world!";
})
// equivalent to:
t(() => 'string')

在第二种方法中,您不是将 string 传递给外部 t,而是传递 function , 调用时, returns 'string'.

t(() => 'string')
t(someFunction)
'function'