JSLint: Using a function before it's defined error
I'm using JSLint to verify most of my external Javascript files, but the largest amount of errors I'm getting is from functions being used before they're defined.
Is this really an issue I should worry about ?
It seems Firefox, IE7 and Chrome don't care. Functions like the popular init()
(which I use often) normally stick at the top as that makes sense to me (I like to pretend it's analogous to main()
) will, according to JSLint, need to be pushed to the bottom of the file.
If you declare functions using the function
keyword, you can use them before they're declared. However, if you declare a function via another method (such as using a function expression or the Function
constructor), you have to declare the function before you use it. See this page on the Mozilla Developer Network for more information.
Assuming you declare all your functions with the function
keyword, I think it becomes a programming-style question. Personally, I prefer to structure my functions in a way that seems logical and makes the code as readable as possible. For example, like you, I'd put an init
function at the top, because it's where everything starts from.
由于这是最高评分的谷歌命中,其他人可能在jslint工具中首先看不到它,所以有一个选项叫做“允许错误定义”,允许你隐藏这种类型的错误。
/*jslint latedef:false*/
If you're using jshint you can set latedef
to nofunc
, which will ignore late function definitions only.
Documentation - http://www.jshint.com/docs/options/#latedef
Example usage:
/* jshint latedef:nofunc */
noop();
function noop() {}
Hope this helps.
链接地址: http://www.djcxy.com/p/19052.html上一篇: 如何将现有的目录树添加到Visual Studio中的项目中?
下一篇: JSLint:在定义错误之前使用函数