函数在定义之前使用 - JSLint [英] Function was used before it was defined - JSLint
问题描述
JSLint不喜欢这段代码说''b'在定义之前就被使用了
JSLint does not like this code saying "'b' was used before it was defined"
var a = function () {
b();
},
b = function () {
alert("Hello, world!");
};
a();
但对此非常满意
var a, b;
a = function () {
b();
};
b = function () {
alert("Hello, world!");
};
a();
但我不是在我的第二个代码片段中定义任何内容。我只是声明变量b。
But I am not defining anything in my second code snippet. I am merely declaring variable b.
那么为什么JSLint这样做呢?我有理由首先声明我的所有功能吗?
So why is JSLint doing this? Is there a reason I should be declaring all my functions first?
PS我明白我可以改变a和b的顺序,但在实际项目中我的功能是事件处理程序,有时他们互相调用,所以这可能是不可能的。
PS I understand I could have just changed order of a and b, but in real project my functions are event handlers and sometimes they call each other, so it is probably not possible.
推荐答案
如果您的代码编写得很好并且对象 - 面向,可以在使用之前声明您的功能。但是JSLint设置了许多与应用程序功能无关的标准,我真的怀疑是否有任何性能影响以某种方式声明函数。
If your code is well-written and object-oriented, it is possible to declare your functions before they are used. But JSLint sets a lot of standards that really have little relevance to the functionality of your application, and I really doubt there are any performance implications declaring functions one way or the other.
这篇关于函数在定义之前使用 - JSLint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!