为什么用括号括起来的函数声明不能​​访问?

Why can't function declaration be accessed after enclosed in parentheses?

无法再通过名称访问函数声明:

(function log(){
  console.log('print some information')
})
log()

代码抛出引用错误。我对此的理解是:函数 log 不存在于任何内部范围内,为什么我不能用它的名字来调用它?

why can't I call it by its name?

当您在 function 两边加上括号时,您会在 () 之间得到一个 function expression, and not a function declaration, as the parser expects to see an expression 而不是声明。函数表达式名称只能在该函数本地访问:

If you want to refer to the current function inside the function body, you need to create a named function expression. This name is then local only to the function body (scope)

- MDN

这同样适用于 !+ 等其他运算符,而不仅仅是 (),这会使您的函数被视为函数表达式。

如果你想做一个函数声明,你可以删除括号:

function log(){
  console.log('print some information');
}
log();