jsLint error: “somefunction() was used before it was defined”

我是研究僧i 提交于 2019-11-27 17:15:49

问题


Why does JSLint complain if something uses a function that hasn't been defined already? The point is that the function is defined -- and if that something calls that function, that function exists and things will work.

Take a look at the code below:

function foo()
{
   // calls bar()
};

function bar()
{
   // calls foo()
};

There is no way to organize the 2 methods in such a way that it would make JSLint happy. How do I deal with this issue?


回答1:


See this answer:

Contending with JS "used before defined" and Titanium Developer

Basically, if you use the foo = function() { ... } form, you can declare var foo, bar; at the top to avoid the JSLint errors.




回答2:


JSLint can't deal with this as far as I know, but JSHint, based on JSLint, tackles this problem in a proper manner.

Just use the "latedef" property and set it to "false". In case you nevertheless want to detect these kind of problematic variable definitions, but do want to use function expressions and allow hoisting of these functions, you can set "latedef" : "nofunc".

Check it out here.




回答3:


I have just dealt with a problem that was very similar to this and the problem was my script was in place after the function call,

function zzzzz () {
   aaaaa();
   ccccc();
  }

function aaaaa() {
 blah = bla blah blah;
 }
function bbbbb() {
 blah = bla blah blah;
 }
function ccccc() {
 blah = bla blah blah;
 }

so i placed the function call after the script and it cured the problem, so basic i couldn't see the answer for days sorted now, so give it a try

function aaaaa() {
 blah = bla blah blah;
 }
function bbbbb() {
 blah = bla blah blah;
 }
function ccccc() {
 blah = bla blah blah;
 }

function zzzzz () {
   aaaaa();
   ccccc();
  }

Goood Luck and i hope this helps



来源:https://stackoverflow.com/questions/7425147/jslint-error-somefunction-was-used-before-it-was-defined

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