Firefox: function hoisting error

后端 未结 1 1070
萌比男神i
萌比男神i 2021-01-12 22:54

I used to assume that functions always get hoisted to the top of any block of JavaScript code.

For example, this works:

document.addEventListener(\         


        
相关标签:
1条回答
  • 2021-01-12 23:37

    It appears this has been an issue for quite a while - here's a reference from 2011: http://statichtml.com/2011/spidermonkey-function-hoisting.html

    Apparently Firefox will happily hoist function declarations OUTSIDE of a block, but doesn't do so INSIDE a block. The author of the linked article argues this is (while unexpected) compliant with the ECMA-262 spec, which only allows statements inside of a block...as a function declaration is not a statement. However, I'll note that Firefox happily allows function declarations inside of a block - it merely refuses to hoist them.

    0 讨论(0)
提交回复
热议问题