Do inner functions in JavaScript get hoisted? How do the scoping rules apply to them?

房东的猫 提交于 2019-11-27 15:23:23
if (…) {
    function …() {            
        …
    }
}

is invalid ECMAScript. Function declarations must be on the top level of function or program bodies, inside of blocks their behaviour is implementation-dependent. Firefox does execute this function statement conditionally, other browsers do hoist it like a normal function declaration. Move it outside the if-statement, or assign a function expression.

The declaration of inner is hoisted to the top of the outer function. However, its value is only set if a == 1.

When outer() is called with a different value, the call to inner(456) fails as inner is still set to undefined.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!