[英]JavaScript JSHint - 'close' is defined but never used
我不知道为什么,但是我用作函数名称的某些单词出现了被定义但从未使用过的错误信息。
例如,下面的代码返回错误:
// I am using AngularJS
$scope.close = close;
function close() {
/* Code here */
}
但这不是:
// I am using AngularJS
$scope.close2 = close2;
function close2() {
/* Code here */
}
错误是在行function close()
。 为什么这么特别的名字? 如何消除此错误?
(注意:答案已经过大量编辑)
John Papa说在latedef
中使用latedef
,并且至少隐式地忽略了JSLint问题。 ( 在这里定义了 latedef
。)
我认为,这里有一个很好玩的解决方案(请参见下文),其中包括Papa建议的样式的优点,以及在JSHint 和 JSLint中插入的代码。
close2
抱怨抱怨close2
是“错误的”。 JSLint完全按照您的期望进行捕获。
close
但不close2
是close2
问题 对于它的价值,如果你粘贴代码(JSLint的格式化Pasteee既密切与close2 这里 )到JSLint.com , 双方 close
和close2
导致错误。 如果您没有看到close2
错误, close2
我想这是JSHint的问题,但是确切地了解通过JSHint(在上下文中)了解的内容会更有用。
如此close
并不是JSLint的特殊名称。 我想在上下文中查看您的“实际”代码,看看JSLint是否会说类似的话
只是要清楚,这在JSLint.com上中断了 :
/*jslint sloppy:true, white:true */
/*global $scope */
$scope.close2 = close2;
function close2() {
return "something";
}
这将产生'close2' was used before it was defined. $scope.close2 = close2;
'close2' was used before it was defined. $scope.close2 = close2;
我相信,如果您想知道JSHint为什么会中断,我们可以进行JSHint代码开发,但是至少要回答您的JS L int标记,您所看到的行为并没有发生。
可以在您正在此处讨论的内容中看到此SO答案 , John Papa表示要在latedef
中使用latedef
。 解决掉毛问题的一种方法是先忽略Papa并定义函数 ,但是,正如您在评论中提到的那样 ,这并不理想。
所以这是我能想到的最好的折衷方案...
绝对可以使JSHint错误静音,因为导致该错误的代码不再存在。 如果我正在做Angular并且需要遵循Papa风格,那就是我为保持Crockford的祝福而要做的事情。
例:
(function () {
'use strict';
// 1. Declare your function names. Minimally spammy!
var theController;
// 2. Directive
angular
.module('myApp')
.controller('myAppCtrl', theController);
// 3. *Define* the functions. No `latedef` needed, and JSLint compliant.
// Keeps "the list of calls at the top of the page" and allows you to
// "jump to each definition if you need more details". QED? ;^)
theController = function () {
return "so jslint doesn't complain about empty blocks";
};
}());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.