为什么JSLint抱怨:“someFunction()在定义之前就被使用了”? [英] WHY JSLint complains: "someFunction() was used before it was defined"?

查看:160
本文介绍了为什么JSLint抱怨:“someFunction()在定义之前就被使用了”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义之前使用了搜索JSLint错误我发现了这些错误:

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?

这些答案都没有为什么显示错误。

None of those answers WHY the error is shown.

根据 ECMA-262规范函数在执行开始之前进行评估,因此使用函数关键字声明的所有函数都可用于与它们所在位置相同的所有代码。声明(假设它们在该范围内是可访问的)。

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

这又称为提升

Douglas Crockford似乎认为在使用它的代码之前声明每个函数更好,无论提升效果如何。

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

根据他的 StackOverflowNewbie -somefunction-被使用的先于它,是画质问题,这引发了一些代码组织问题。更不用说像我这样的人喜欢在main / init代码下声明他们的函数。

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.

在这些问题上有一些方法可以避免或修复错误,例如使用函数表达式和函数声明。但他们都没有告诉我错误的原因。甚至不 Crockford的网站

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.

为什么在声明之前调用函数是错误的,即使它是使用<$ c $声明的c> function keyword?

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

在JSLint上下文中使用函数表达式而不是函数声明是否更好?如果首选,为什么?

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

寻找类似的答案:


  • Crockford是暴君

  • 只是Crockford的意见

谢谢:*

推荐答案

这不是错误(你的代码毕竟工作了。对此的关注是在使用后声明函数会降低代码的可读性。阅读代码的人可能认为此函数在外部范围内声明。如果你覆盖流行的函数/构造函数,如 Date

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

这篇关于为什么JSLint抱怨:“someFunction()在定义之前就被使用了”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆