Avoid-Function-Declarations-In-Blocks¶
Sourcery rule id: avoid-function-declarations-in-blocks
¶
Description¶
Avoid function declarations, favouring function assignment expressions, inside blocks.
Match¶
if (x) {
function foo() {}
}
Explanation¶
Function declarations may be hoisted in Javascript, but the behaviour is inconsistent between browsers. Hoisting is generally confusing and should be avoided. Rather than using function declarations inside blocks, you should use function expressions, which create functions in-scope.