javascript jsLint 错误:“somefunction() 在定义之前被使用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7425147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:05:32  来源:igfitidea点击:

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

javascriptjslint

提问by StackOverflowNewbie

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.

如果某些东西使用尚未定义的函数,为什么 JSLint 会抱怨?关键是函数已经定义——如果某个东西调用了那个函数,那么那个函数就存在并且一切都会起作用。

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?

没有办法以让 JSLint 满意的方式组织这 2 种方法。我该如何处理这个问题?

采纳答案by MrTrick

See this answer:

看到这个答案:

Contending with JS "used before defined" and Titanium Developer

与“定义前使用”的 JS 和 Titanium Developer 竞争

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

基本上,如果您使用foo = function() { ... }表单,您可以var foo, bar;在顶部声明以避免 JSLint 错误。

回答by erik van nieuwburg

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

据我所知,JSLint 无法处理这个问题,但是基于 JSLint 的 JSHint 以适当的方式解决了这个问题。

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".

只需使用“latef”属性并将其设置为“false”。如果您仍然想检测这些有问题的变量定义,但确实想使用函数表达式并允许提升这些函数,您可以设置“latef”:“nofunc”。

Check it out here.

检查它在这里

回答by Morph

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

祝你好运,我希望这会有所帮助