WHY JSLint complains: "someFunction() was used before it was defined"?

Posted by 7hi4g0 on Stack Overflow See other posts from Stack Overflow or by 7hi4g0
Published on 2013-12-26T14:39:03Z Indexed on 2014/06/06 9:25 UTC
Read the original article Hit count: 194

Filed under:
|
|

Searching for the JSLint error "was used before it was defined" i've found these:

Problem

None of those answers WHY the error is shown.

Elaboration

According to the ECMA-262 Specification functions are evaluated before execution starts, hence all functions declared using the function keyword are available to all the code idenpendent of the place they were declared (assuming they are acessible on that scope).

This is otherwise known as hoisting.

Douglas Crockford seems to think it is better to declare every function before the code that uses it regardless of the hoisting effect.

According to StackOverflowNewbie in his question, this raises some code organization problems. Not to mention some people, like me, prefer to declare their functions underneath the main/init code.

On those questions there are some ways to avoid or fix the error, such as using function expressions vs function declarations. But none of them showed me the reason of the error. Not even Crockford's site.

Question(s)

Why is it an error to call a function before the declaration, even if it was declared using the function keyword?

Is it better to use function expressions instead of function declaration in the JSLint context? If one is preferred, why?

Note

Not looking for answers like:

  • Crockford is a tyrant
  • Is just Crockford's opinion

Thank you :*

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jslint