自调用函数是如何提升的?

How are self-invoking functions hoisted?

请参阅此代码示例:

var myFunc = function () {
  alert("hello world");
})();

这是一个函数表达式,没有提升。所以我在这里的假设是它仅在代码到达其在代码中的实际位置时运行。这是真的吗?

This is a function expression, which is not hoisted.

是的。函数表达式未提升(自调用或其他方式)。

此外,任何地方都没有对存储的函数的引用,所以首先没有任何东西可以提升。


So my presumption here is that it runs only…

函数声明被提升,但这与它们何时被提升无关运行。恰好与函数名匹配的变量名得到它的值(所以它可以用来调用函数)。

一个函数被调用时只需要 运行 秒。

在你的代码中,then是函数体之后的()被处理的时候。


与所有 var 声明一样,myFuncvar 声明将被托管。它会得到一个undefined的初始值,然后,当函数运行s时,它会得到那个函数的return值(也就是undefined)。

是的,你是对的。

Function expressions in JavaScript are not hoisted, unlike function declarations. You can't use function expressions before you define them

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function