Javascript中的空函数 [英] Empty functions in Javascript
问题描述
如果我有这样的话:
var blah = function(){};
然后在代码中使用blah,JSLint提示说删除空块是什么?
我不知道 jsLint
是怎么想的,但如果这是一个问题,你需要一个解决方案,然后你可以做类似以下的事情:
var blah = function(){return undefined; }; //或者只是回来;
更新:我认为, Bergi
的猜测是正确的,因为在<$ c的 jslint 网站上$ c>必需块部分:
JSLint期望if,while,do和for语句将被生成使用
块{即括在括号中的语句} .JavaScript允许
an if如下所示:if(condition)statement;该形式是
已知会导致错误许多程序员使用相同代码处理
的项目。这就是为什么JSLint期望使用
块:
if(condition ){陈述; }
经验表明此表单更具弹性。
所以,它可能只是检查空块 {}
并使空白函数无效。 / p>
If I have something like this:
var blah = function() { };
and then later in code blah is being used, what is the JSLint hint that says remove the empty block?
I don't know what jsLint
thinks but if this is a problem and you need a solution then you can do something like the following:
var blah = function() { return undefined; }; // or just return;
Update : I think, Bergi
's guess is right because, on the jslint site in the Required Blocks
section :
JSLint expects that if, while, do and for statements will be made with blocks {that is, with statements enclosed in braces}.JavaScript allows an if to be written like this:if (condition) statement;That form is known to contribute to mistakes in projects where many programmers are working on the same code. That is why JSLint expects the use of a block:
if (condition) { statements; }
Experience shows that this form is more resilient.
So, It probably just checks for empty blocks { }
and invalidate the blank function.
这篇关于Javascript中的空函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!