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

不打扰是莪最后的温柔 提交于 2020-01-02 04:36:04

问题


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

  • JSLint: Using a function before it's defined error
  • Function was used before it was defined - JSLint
  • JSLint: was used before it was defined
  • jsLint error: “somefunction() was used before it was defined”
  • jslint - Should we tolerate misordered definitions?

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 :*


回答1:


It is NOT an error (your code does work after all). The concern with this is that declaring function after it has been used decreases readibility of the code. Someone reading your code may think this function is declared somewhere in outer scope. This can be especially confusing, if you overwrite popular functions/constructors like Date




回答2:


Generally, if you're going to do what jshint/jslint say, and define functions before their use, then there's no overwhelming reason (other than aesthetic) to prefer function declarations over function expressions.

Here's my opinion: Declaring functions after they're used is bad, unless it's very clear that you're referring to a function that's declared in the same scope. This can either be a result of good naming or proximity.

For example, you're returning a reference to the declared function immediately before it's declared. Here, you've clarified the intent of your outer function, which is equally as important, and the reader knows they don't have to scan downward to see if there is additional code specific to the outer scope.

That said, I also think sprinkling function declarations amongst your outer scope's code of intent is also horrible for readability, but maybe I'm a terrible person.



来源:https://stackoverflow.com/questions/20786621/why-jslint-complains-somefunction-was-used-before-it-was-defined

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